这似乎很简单,但我似乎无法让它工作。XSLT迭代子节点
我的XML看起来是这样的:
<bsar:BSAForm>
<bsar:SubjectInformation>
<bsar:LastNameOrNameOfEntity> Obama</bsar:LastNameOrNameOfEntity>
<bsar:AddressBlock>
<ucc:Address> 9 Dale Rd</ucc:Address>
<ucc:City> Woodbury</ucc:City>
</bsar:AddressBlock>
<bsar:AddressBlock>
<ucc:Address> 123 Fake St</ucc:Address>
<ucc:City> Springfield</ucc:City>
</bsar:AddressBlock>
</bsar:SubjectInformation>
</bsar:BSAForm>
我需要遍历这两个元素和显示内容。我的XSLT是这样的:
<xsl:template match=/bsar:BSAForm">
<xsl:apply-templates select="bsar:SubjectInformation"/>
</xsl:template>
<xsl:template match="bsar:SubjectInformation">
<xsl:text xml:space="preserve">4A</xsl:text>
<xsl:text xml:space="preserve">00001</xsl:text>
<xsl:value-of select="bsar:LastNameOrNameOfEntity"/>
<xsl:value-of select="'
'"/> <!-- new line -->
<xsl:apply-templates select="bsar:AddressBlock"/>
</xsl:template>
<xsl:template match="bsar:AddressBlock">
<xsl:variable name="Addr" select="../bsar:AddressBlock"/>
<xsl:text xml:space="preserve">4B</xsl:text>
<xsl:text xml:space="preserve">00001</xsl:text>
<xsl:value-of select="$Addr/ucc:Address"/>
<xsl:value-of select="$Addr/ucc:City"/>
<xsl:value-of select="'
'"/> <!-- new line -->
</xsl:template>
输出应该如下:
4A 00001 Obama
4B 9 Dale Rd Woodbury
4B 123 Fake St Springfield
而是输出出来是这样的:
4A 00001 Obama
4B 9 Dale Rd Woodbury
4B 9 Dale Rd Woodbury
我已经尝试了许多不同的方法来做到这一点,每个使用一个,使用每个像这样:
<xsl:variable name="header" select="."/>
<xsl:for-each select="following-sibling::bsar:TelephoneBlock[ancestor::bsar:SubjectInformation[1] = $header]">
或者甚至传递一个计数器从一个for循环和使用,以访问指定的元件是这样的:
<xsl:for-each select="bsar:TelephoneBlock">
<xsl:variable name="Index">
<xsl:number count="bsar:TelephoneBlock" />
</xsl:variable>
<xsl:call-template name="SubjectPhone">
<xsl:with-param name="$Index"/>
</xsl:call-template>
</xsl:for-each>
<xsl:template name="SubjectPhone">
<xsl:param name="Index"/>
<xsl:variable name="Telephone" select="../bsar:TelephoneBlock[$Index]"/>
...
</xsl:template>
在所有这些情况下,它是两次显示所述第一地址。请让我知道你是否注意到我做错了什么。
在此先感谢。
你说得对。愿你活到一千岁。 – 2013-02-22 15:48:54