XSLT编号-分组?
我正在努力做到以下几点:XSLT编号-分组?,xslt,Xslt,我正在努力做到以下几点: <body> <div> <p> text<note/>texttext<note/>text </p> <p> text<note/>text </p> </div> <div> text<note/>texttext<note/>text
<body>
<div>
<p> text<note/>texttext<note/>text </p>
<p> text<note/>text </p>
</div>
<div>
text<note/>texttext<note/>text
</div>
</body>
文本文本文本文本
文本
文本文本文本文本
应该导致
<body>
<div>
<p> text<note n="1"/>texttext<note n="2"/>text </p>
<p> text<note n="3"/>text </p>
</div>
<div>
text<note n="1"/>texttext<note n="2"/>text
</div>
</body>
文本文本文本文本
文本
文本文本文本文本
如您所见,我希望对div下的所有注释进行编号,而不考虑父节点。因此,可以在div下以任何方式构造注释。
但是,我无法通过使用xsl:number找到解决方案。任何帮助都将不胜感激
编辑:非常感谢DRCB的解决方案。我已经对它进行了调整,以便它也可以通过使用标识模板用于复杂的嵌套
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="div//note">
<note>
<xsl:attribute name="n">
<xsl:value-of select="count(preceding::note) - count(preceding::div//note) + 1"/>
</xsl:attribute>
<xsl:value-of select="."/>
</note>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
变换形式:
<body>
<any>
<div>
<p>
<p> text<note/>texttext<note/>text </p>
</p>
<p> text<note/>text </p>
</div>
</any>
<div> text<note/>texttext<note/>text </div>
</body>
文本文本文本文本
文本
文本文本文本文本
致:
文本文本文本文本
文本
文本文本文本文本
我相信可能有更好的解决方案,但这对我来说很有效。我发现了以下快速解决方法:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalogp -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="div">
[div <xsl:apply-templates/>]
</xsl:template>
<xsl:template match="note">
[note n=<xsl:value-of select="count(preceding::note) - count(preceding::div//note) + 1"/>]
</xsl:template>
</xsl:stylesheet>
[部门]
[注n=]
但是,它只适用于“普通”div结构,没有复杂的嵌套
您可以在这里测试它:使用源xml。使用
xsl:number的解决方案。
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="note">
<xsl:variable name="vNum">
<xsl:number level="any" count="note" from="/*/div"/>
</xsl:variable>
<note n="{$vNum}">
<xsl:apply-templates/>
</note>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的XML文档时:
<body>
<div>
<p> text<note/>texttext<note/>text </p>
<p> text<note/>text </p>
</div>
<div>
text<note/>texttext<note/>text
</div>
</body>
<body>
<div>
<p> text<note n="1"/>texttext<note n="2"/>text </p>
<p> text<note n="3"/>text </p>
</div>
<div>
text<note n="1"/>texttext<note n="2"/>text
</div>
</body>
文本文本文本文本
文本
文本文本文本文本
生成所需的正确结果:
<body>
<div>
<p> text<note/>texttext<note/>text </p>
<p> text<note/>text </p>
</div>
<div>
text<note/>texttext<note/>text
</div>
</body>
<body>
<div>
<p> text<note n="1"/>texttext<note n="2"/>text </p>
<p> text<note n="3"/>text </p>
</div>
<div>
text<note n="1"/>texttext<note n="2"/>text
</div>
</body>
文本文本文本文本
文本
文本文本文本文本
解释:适当使用
xsl:number
的from
属性尝试以下解决方案:[XSLT increment variable][1][1]:您应该将您的解决方案作为您自己问题的答案发布。不要把它编辑成问题本身。我必须等24小时才能回答我自己的问题。这就是我编辑这篇文章的原因。@Chris:一个只使用xsl:number
?:)的更简单的解决方案怎么样非常感谢。我目前正试图通过使用标识模板(即match=“node()|@*”)来调整您的解决方案。我认为这是可行的,因此它也可以用于复杂的嵌套。