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

sspsujit

如何获得如下示例的输出?我想根据它们的级别和位置为部分元素生成 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

XSLT通过元素匹配从xml复制元素

来自分类Dev

ID或Class元素中的xslt值

来自分类Dev

如何使用XSLT匹配元素并输出HTML?

来自分类Dev

通过在XSLT中开始/结束属性值来计数元素

来自分类Dev

如何通过引用元素的id来获取元素的innerText?

来自分类Dev

如何使xslt中的元素求和

来自分类Dev

XSLT中的模式匹配元素名称

来自分类Dev

如何通过jquery动态生成的id和值来匹配元素

来自分类Dev

XSLT匹配元素并填充

来自分类Dev

XSLT,XPath:如何通过引用其子元素的属性来获取父元素的属性?

来自分类Dev

Xslt:如何通过Xslt样式表中的类选择器选择xml元素?

来自分类Dev

搜索 XML 中的元素,如果它前面是通过 XSLT 的元素

来自分类Dev

如何通过xslt(复杂查询)获取元素的值

来自分类Dev

如何通过XSLT删除和添加选择元素?

来自分类Dev

如何通过XSLT自动增加元素/变量值?

来自分类Dev

如何对XSLT中多个元素的乘积求和?

来自分类Dev

如何正确访问 XSLT 中的祖先元素?

来自分类Dev

如何使用xslt 1.0删除特定匹配的元素

来自分类Dev

如何将元素的所有内容与XSLT匹配?

来自分类Dev

XSLT按ID获取数组元素

来自分类Dev

Javascript Redux-如何通过ID从存储中获取元素

来自分类Dev

如何在ASP.NET中通过ID获取元素?

来自分类Dev

如何通过名称在js中设置元素的ID?

来自分类Dev

如何在 ShadowDOM 中通过 id 查找元素?

来自分类Dev

如何通过其ID选择元素的曾孙

来自分类Dev

如何通过id更新几个元素?

来自分类Dev

如何通过ID Javascript选择元素范围

来自分类Dev

如何通过GWT DockLayoutPanel的ID获取元素?

来自分类Dev

Web组件,如何通过Id获取元素?

Related 相关文章

热门标签

归档