Xslt 模板内部自动增量
我有下面的XMLXslt 模板内部自动增量,xslt,xpath,xslt-2.0,Xslt,Xpath,Xslt 2.0,我有下面的XML <?xml version="1.0" encoding="UTF-8"?> <uaeccc> <nd> <AN>-2-0001</AN> <h2>Ttle2</h2> </nd> <nd> <h1>Ttle1</h1> <h2>Ttle2</h2&g
<?xml version="1.0" encoding="UTF-8"?>
<uaeccc>
<nd>
<AN>-2-0001</AN>
<h2>Ttle2</h2>
</nd>
<nd>
<h1>Ttle1</h1>
<h2>Ttle2</h2>
<h3>Part 1 Contracts</h3>
<h4>par2 sub contractts</h4>
</nd>
<nd>
<h1>Ttle1</h1>
<h2>Ttle2 </h2>
<h3>Part 1 Contracts</h3>
<h4>Part 2 Sub contracts</h4>
</nd>
<nd>
<h1>Ttle1</h1>
<h2>Ttle2 </h2>
<h3>Part 1 Contracts</h3>
<h4>part 2 sub contracts</h4>
</nd>
</uaeccc>
我无法理解您的代码。FWIW,当应用于示例输入时,以下样式表将生成所需的输出(将结果包装到单个根元素中除外):
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/uaeccc">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="nd">
<div class="section-sect1">
<xsl:variable name="sectionnum">
<xsl:number count="nd|h4" level="any"/>
</xsl:variable>
<a name="CH_02-SEC-{$sectionnum}"/>
<div class="section-title">
<xsl:copy-of select="h3"/>
</div>
<xsl:apply-templates select="h4"/>
</div>
</xsl:template>
<xsl:template match="h4">
<div class="section-sect1">
<xsl:variable name="sectionnum">
<xsl:number count="nd|h4" level="any"/>
</xsl:variable>
<a name="CH_02-SEC-{$sectionnum}"/>
<div class="section-title2">
<xsl:copy-of select="."/>
</div>
</div>
</xsl:template>
</xsl:stylesheet>
要知道你会做什么并准确计算,这是非常不清楚的。实际上,
是相对于当前正在处理的
的“计数”,因此您无法计算H1H2等等。请解释1)您希望如何组织输出文档(看起来您希望执行一些分组)和2)编号必须依赖哪些元素(似乎您希望计算h3和h4)。谢谢。你好@potame,是的,你说得对。在这里,最初我只是从计数开始,后来我发现如果有h3
或h4
,那么计数应该基于h3
或h4
。最后,你根本不输出h1
和h2
,是吗?Nop@potame,我不输出h1
和h2
谢谢您的解决方案:-)
<div class="section-sect1">
<a name="CH_02-SEC-1"></a>
<div class="section-title"></div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-2"></a>
<div class="section-title">
<h3>Part 1 Contracts</h3>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-2"></a>
<div class="section-title2">
<h4>par2 sub contractts</h4>
</div>
</div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-3"></a>
<div class="section-title">
<h3>Part 1 Contracts</h3>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-3"></a>
<div class="section-title2">
<h4>Part 2 Sub contracts</h4>
</div>
</div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-4"></a>
<div class="section-title">
<h3>Part 1 Contracts</h3>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-4"></a>
<div class="section-title2">
<h4>part 2 sub contracts</h4>
</div>
</div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-1"></a>
<div class="section-title"></div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-2"></a>
<div class="section-title">
<h3>Part 1 Contracts</h3>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-3"></a>
<div class="section-title2">
<h4>par2 sub contractts</h4>
</div>
</div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-4"></a>
<div class="section-title">
<h3>Part 1 Contracts</h3>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-5"></a>
<div class="section-title2">
<h4>Part 2 Sub contracts</h4>
</div>
</div>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-6"></a>
<div class="section-title">
<h3>Part 1 Contracts</h3>
</div>
<div class="section-sect1">
<a name="CH_02-SEC-7"></a>
<div class="section-title2">
<h4>part 2 sub contracts</h4>
</div>
</div>
</div>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/uaeccc">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="nd">
<div class="section-sect1">
<xsl:variable name="sectionnum">
<xsl:number count="nd|h4" level="any"/>
</xsl:variable>
<a name="CH_02-SEC-{$sectionnum}"/>
<div class="section-title">
<xsl:copy-of select="h3"/>
</div>
<xsl:apply-templates select="h4"/>
</div>
</xsl:template>
<xsl:template match="h4">
<div class="section-sect1">
<xsl:variable name="sectionnum">
<xsl:number count="nd|h4" level="any"/>
</xsl:variable>
<a name="CH_02-SEC-{$sectionnum}"/>
<div class="section-title2">
<xsl:copy-of select="."/>
</div>
</div>
</xsl:template>
</xsl:stylesheet>