我有一个类似的XML示例:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<location id="1">
<address>1600 Pennsylvania Avenue</address>
<address>211B Baker Street</address>
</location>
<location id="1">
<address>17 Cherry Tree Lane</address>
</location>
<location id="2">
<address>350 5th Avenue</address>
</location>
</root>
我想生成看起来像的输出:
<?xml version="1.0" encoding="utf-8"?>
<result>
<location id="1">
<address addressId="1">1600 Pennsylvania Avenue</address>
<address addressId="2">211B Baker Street</address>
</location>
<location id="1">
<address addressId="3">17 Cherry Tree Lane</address>
</location>
<location id="2">
<address addressId="1">350 5th Avenue</address>
</location>
</result>
这样addressId
反映了具有相同address
属性的所有location
个实例中id
的序列。
我在想<xsl:number>
是我的答案,但我的尝试失败了:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/root">
<result>
<xsl:for-each select="location">
<location>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:for-each select="address">
<address>
<xsl:attribute name="addressId">
<xsl:number count="//location[@id = ../@id]/address" level="any" />
</xsl:attribute>
<!--
The rest are just my debugging attempts;
curiously addressId3 and addressId4 return
different values?
-->
<!--
<xsl:attribute name="addressId2">
<xsl:number count="//location[@id = parent::location/@id]/address" level="any" />
</xsl:attribute>
<xsl:attribute name="addressId3">
<xsl:value-of select="count(//location[@id=../@id]/address)" />
</xsl:attribute>
<xsl:variable name="locId">
<xsl:value-of select="../@id" />
</xsl:variable>
<xsl:attribute name="addressId4">
<xsl:value-of select="count(//location[@id=$locId]/address)" />
</xsl:attribute>
<xsl:attribute name="addressId5">
<xsl:number count="//location[@id = '1']/address" level="any" />
</xsl:attribute>
-->
<xsl:value-of select="." />
</address>
</xsl:for-each>
</location>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:5)
以下是一种解决方法,只需计算具有address
父级且具有相同location
属性的前id
个元素:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="location/address">
<xsl:copy>
<xsl:attribute name="addressId">
<xsl:value-of select="count(preceding::address[../@id = current()/../@id]) + 1"/>
</xsl:attribute>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:3)
重新<xsl:number>
:为了让它在每个位置重新启动/ @ id更改,您可以指定:
<xsl:number from="location[@id!=preceding-sibling::location[1]/@id]" level="any" />
请注意,这与使用相同父属性计算前面的项目不同;如果您将另一个位置@ id = 1附加到当前列表,差异将变得明显。