2008-11-05 53 views
0

我有XSL父子问题

<elig> 
<subscriber code="1234"/> 
    <date to="12/30/2004" 
     from="12/31/2004"/> 
     <person name="bob" 
      ID="654321"/> 
     <dog type="labrador" 
      color="white"/> 
    <location name="hawaii" 
       islandCode="01"/> 
</subscriber> 
</elig> 

一个XML构建XSL我有:

<xsl:template match="subscriber"> 
<xsl:for-each select="date"> 
    <xsl:apply-templates match="person" /> 
    <xsl:apply-templates match="location" /> 
    <xsl:apply-templates match="dog" /> 
</xsl:for-each> 
</xsl:template> 

我的问题是我需要的位置,挡在了人与狗之间块。我试过../,但它不起作用。我简化了这个主要但重点。我似乎无法记住我需要放置在位置前面才能使其工作。谢谢。

+0

一件事牢记的是,`的xsl:for-each`改变上下文节点的for-each元素内将在所选择的节点集的当前节点for-each。这就是为什么你需要父轴(../)。 – ChuckB 2008-11-05 18:42:41

回答

1

首先你XML仍然不健全,我实际上无法理解为什么你要循环使用<date/> t ags - <subscriber/>内只有一个<date/>标签(假设第一个<subscriber/>不应该自行关闭)。

使用XPath时,您一定要考虑调用XPatch的上下文。下面应该这样做(当我对你的数据结构的假设是正确的):

<xsl:template match="subscriber"> 
<xsl:for-each select="date"> 
    <!-- from here on we're in the context of the date-tag --> 
    <xsl:apply-templates match="../person" /> 
    <xsl:apply-templates match="../location" /> 
    <xsl:apply-templates match="../dog" /> 
</xsl:for-each> 
</xsl:template> 
+0

我有一堆错误的东西,一旦修正../location的工作,只是其中的一个..... – Primetime 2008-11-05 18:17:36

1

我修改一个错字在您的示例XML:

<elig> 
<subscriber code="1234"> 
    <date to="12/30/2004" from="12/31/2004"/> 
     <person name="bob" ID="654321"/> 
     <dog type="labrador" color="white"/> 
    <location name="hawaii" islandCode="01"/> 
</subscriber> 
</elig> 

并使用此样式的一切工作得很好:

<xsl:template match="subscriber"> 
<xsl:for-each select="date"> 
    <xsl:apply-templates select="../person" /> 
    <xsl:apply-templates select="../location" /> 
    <xsl:apply-templates select="../dog" /> 
</xsl:for-each> 
</xsl:template> 

<xsl:template match="person">person</xsl:template> 
<xsl:template match="location">location</xsl:template> 
<xsl:template match="dog">dog</xsl:template> 

输出是:

personlocationdog 
0

在这种情况下,是不是更符合逻辑移动for-each循环外的应用模板调用?由于人,地点和狗元素是订户的子女,因此应在订户范围内处理,而不是在日期范围内处理。

即:

<xsl:template match="subscriber"> 
    <xsl:for-each select="date"> 
    <!-- Perform the processing of the date tags here--> 
    </xsl:for-each> 

    <xsl:apply-templates match="person" /> 
    <xsl:apply-templates match="location" /> 
    <xsl:apply-templates match="dog" /> 
</xsl:template> 
1
<xsl:template match="subscriber"> 
    <xsl:apply-templates match="date" /> 
</xsl:template> 

<xsl:template match="date"> 
    <xsl:apply-templates match="../person" /> 
    <xsl:apply-templates match="../location" /> 
    <xsl:apply-templates match="../dog" /> 
</xsl:template> 

     instead of xsl:for-each on date better practice is having a template match for date.