如何获得如下示例的输出?我想根据它们的级别和位置为部分元素生成 id。请注意<link>
调用匹配 id的结果。
任何帮助将是可观的。
输入:
<book>
<section id="s1">
<section id="s7">
<section id="s9">
</section>
<p>xxxxxx</p>
<section id="s2">
</section>
</section>
<section id="s17">
<section id="s19">
</section>
</section>
</section>
<p>yyyyyyyyy</p>
<section id="s201">
<section id="s190">
<link href="book1:s19"/>
</section>
<section id="s200">
<link href="book1:s2"/>
</section>
</section>
</book>
输出应为:
<book>
<section id="s1" order="1">
<section id="s1-1" order="2">
<section id="s1-1-1" order="3">
</section>
<p>xxxxxx</p>
<section id="s1-1-2" order="3">
</section>
</section>
<section id="s1-2" order="2">
<section id="s1-2-1" order="3">
</section>
</section>
</section>
<p>yyyyyyyyy</p>
<section id="s2" order="1">
<section id="s2-1" order="2">
<a href="s1-2-1"/>
</section>
<section id="s2-2" order="2">
<a href="s1-1-2"/>
</section>
</section>
</book>
通过在 xslt 下实现,我只能生成<section>
元素的 id 。但是无法<a href="{value_of_resulting_section_id matched with id in @href attribute of link}">
在链接元素的@href 中匹配id 的结果部分id 中获得正确的输出。
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml"/>
<xsl:key name="KEY" match="section" use="id" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<book><xsl:apply-templates/></book>
</xsl:template>
<xsl:template match="section">
<xsl:variable name="ORDER" select="count(ancestor::section)+1"/>
<xsl:element name="section">
<xsl:if test="$ORDER=1">
<xsl:attribute name="id"><xsl:text>s</xsl:text>
<xsl:value-of select="count(preceding-sibling::section)+1"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$ORDER=2">
<xsl:attribute name="id"><xsl:text>s</xsl:text>
<xsl:value-of select="($ORDER - 1) + count(parent::section/preceding-sibling::section)"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="count(preceding-sibling::section)+1"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$ORDER=3">
<xsl:attribute name="id"><xsl:text>s</xsl:text>
<xsl:value-of select="($ORDER - 2)"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="count(parent::section/preceding-sibling::section)+1"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="count(preceding-sibling::section)+1"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="order">
<xsl:value-of select="count(ancestor::section)+1"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="link">
<xsl:element name="a">
<xsl:attribute name="id">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<xsl:attribute name="href">
<xsl:value-of select="count(//section[@id=substring-after(@href,':')])"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:transform>
有人有答案请帮忙吗?谢谢...
编号可以用 来完成xsl:number
,交叉引用可以用一个键找到,新数字的计算可以分解成它自己的模板,这样元素和引用的元素都可以用它来计算同一个id:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="xs math map array"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:key name="section-ref" match="section" use="@id"/>
<xsl:template match="section">
<xsl:copy>
<xsl:attribute name="id">
<xsl:apply-templates select="." mode="generate-id"/>
</xsl:attribute>
<xsl:attribute name="order" select="count(ancestor-or-self::section)"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="link[@href]">
<a>
<xsl:attribute name="href">
<xsl:apply-templates select="key('section-ref', substring-after(@href, ':'))" mode="generate-id"/>
</xsl:attribute>
</a>
</xsl:template>
<xsl:template match="section" mode="generate-id">
<xsl:text>s</xsl:text>
<xsl:number level="multiple" format="1-1"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/eiZQaFC是一个在线 XSLT 3 示例,但对于 XSLT 2,如果您删除xsl:mode
并保留身份转换模板,代码应该也能正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句