如何通过匹配xslt中的id来确定元素的级别?

问题描述 投票:0回答:1

如何获得如下例所示的输出?我想根据它们的级别和位置为section元素生成id。请注意<link>的结果,其中匹配的id被调用。

任何帮助都会很明显。

INPUT:

<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>

有人有答案请帮忙吗?谢谢...

xslt xpath
1个回答
0
投票

可以使用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并保留您的身份转换模板,代码也应该正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.