获取每个第一个元素的XPath,并计算所有XML输入的同级出现次数
我需要使用xsl计算XML文档中的每个重复元素,XML的结构未知,因此如果我的XML如下所示获取每个第一个元素的XPath,并计算所有XML输入的同级出现次数,xml,xslt,Xml,Xslt,我需要使用xsl计算XML文档中的每个重复元素,XML的结构未知,因此如果我的XML如下所示 <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>U
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<title>Hide your heart1</title>
<price>9.90</price>
<price>9.90</price>
<year>1988</year>
<year>1988</year>
</cd>
<catalog>
提前谢谢你没有回答我的问题,但还是这样: XSLT1.0
您使用的XSLT版本是什么(1.0、2.0、3.0)以及什么处理器?无论如何,为了好玩,我为这三个版本添加了一个解决方案,非常适合三个标准之间的比较:)@RudramuniTP,谢谢!刚刚添加了XSLT3.0版本。我喜欢“赋值”,因为它(1)显示了XSLT的威力,(2)清楚地显示了版本之间的进展,(3)在这几行中显示了相当多的XSLT概念。
catalog.cd.count=2
catalog.cd[0].title.count=1
catalog.cd[0].artist.count=1
catalog.cd[0].country.count=1
catalog.cd[0].company.count=1
catalog.cd[0].price.count=1
catalog.cd[0].year.count=1
catalog.cd[1].title.count=2
catalog.cd[1].price.count=2
catalog.cd[1].year.count=2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" />
<xsl:template match="*">
<xsl:variable name="nm" select="name()" />
<xsl:if test="not(preceding-sibling::*[name() = $nm])">
<xsl:apply-templates select="ancestor-or-self::*" mode="path" />
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()" />
<xsl:template match="*" mode="path">
<xsl:variable name="nm" select="name()" />
<xsl:variable name="same" select="../*[name() = $nm]" />
<xsl:value-of select="name()" />
<xsl:if test="position() != last()">
<xsl:text>[</xsl:text>
<xsl:value-of select="count(preceding-sibling::*[name() = $nm]) + 1 " />
<xsl:text>]</xsl:text>
</xsl:if>
<xsl:text>.</xsl:text>
<xsl:if test="position() = last()">
<xsl:text>count = </xsl:text>
<xsl:value-of select="count($same)" />
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="text" />
<xsl:template match="*">
<xsl:apply-templates select="
if(preceding-sibling::*/name() = name())
then () else ancestor-or-self::*" mode="path" />
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()" />
<xsl:template match="*" mode="path">
<xsl:variable name="nm" select="name()" />
<xsl:value-of separator="" select="name(),
if(position() = last())
then ('.count = ', count(../*[name() = $nm]), '
')
else ('[', count(preceding-sibling::*[name() = $nm]) + 1, ']', '.')" />
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0" expand-text="yes">
<xsl:output method="text" />
<xsl:mode on-no-match="shallow-skip" />
<xsl:template match="*[not(preceding-sibling::*/name() = name())]" >{
path()!replace(., '^/|Q\{\}|\[1\]$', '')!translate(., '/', '.')
|| '.count = ' || count(../*[name() = current()/name()]) || '
'
}<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
catalog.count = 1
catalog[1].cd.count = 2
catalog[1].cd[1].title.count = 1
catalog[1].cd[1].artist.count = 1
catalog[1].cd[1].country.count = 1
catalog[1].cd[1].company.count = 1
catalog[1].cd[1].price.count = 1
catalog[1].cd[1].year.count = 1
catalog[1].cd[2].title.count = 2
catalog[1].cd[2].price.count = 2
catalog[1].cd[2].year.count = 2