这似乎很简单,但我似乎无法让它工作。
我的 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
我尝试了许多不同的方法来做到这一点,使用一个 for each,使用一个 for each 像这样:
<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>
在所有这些情况下,它都会显示第一个地址两次。如果您发现我做错了什么,请告诉我。
提前致谢。