XSLT:排序的路径跟踪



我有以下输入:

<articles>
  <item name="B">
    <item name="Bb">
      <item name="Bbb"/>
      <item name="Abb"/>
    </item>
    <item name="Ab">
      <item name="Bab"/>
      <item name="Aab"/>
    </item>
  </item>
  <item name="A">
    <item name="Ba">
      <item name="Bba"/>
      <item name="Aba"/>
    </item>
    <item name="Aa">
      <item name="Baa"/>
      <item name="Aaa"/>
    </item>
  </item>
  <item name="D"/>
  <item name="C">
    <item name="Ac"/>
  </item>
</articles>

并且需要以下输出:

A-Aa-Aaa
A-Aa-Baa
A-Ba-Aba
A-Ba-BBa
B-Ab-Aab
B-Ab-Bab
B-Bb-Abb
B-Bb-Bbb
C-Ac
D

也就是说,每条路径都应该明确,并在每个级别上进行排序。我有下面的XSLT,它生成路径,但除了第一级之外,我无法计算排序。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="articles">
    <xsl:apply-templates select="//item[not(item)]">
      <xsl:sort select="ancestor-or-self::item[parent::articles]/@name"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="item">
    <xsl:for-each select="ancestor-or-self::item">
      <xsl:value-of select="@name"/>
      <xsl:choose>
        <xsl:when test="not(item)">
          <xsl:text>&#xA;</xsl:text>
        </xsl:when>
        <xsl:otherwise>-</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

恐怕我目前的做法是走不通的。想法?

请注意,XSLT2.0解决方案(第二部分)更短、更简单


I。这里有一个简单的两步解决方案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/">
  <xsl:variable name="vrtfPass1">
    <xsl:apply-templates/>
  </xsl:variable>
  <xsl:for-each select="ext:node-set($vrtfPass1)/*">
   <xsl:sort/>
   <xsl:value-of select="concat(., '&#xA;')"/>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="item[not(item)]">
     <x>
      <xsl:for-each select="ancestor-or-self::item">
       <xsl:if test="not(position() = 1)">-</xsl:if>
       <xsl:value-of select="@name"/>
      </xsl:for-each>
     </x>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于所提供的XML文档时:

<articles>
  <item name="B">
    <item name="Bb">
      <item name="Bbb"/>
      <item name="Abb"/>
    </item>
    <item name="Ab">
      <item name="Bab"/>
      <item name="Aab"/>
    </item>
  </item>
  <item name="A">
    <item name="Ba">
      <item name="Bba"/>
      <item name="Aba"/>
    </item>
    <item name="Aa">
      <item name="Baa"/>
      <item name="Aaa"/>
    </item>
  </item>
  <item name="D"/>
  <item name="C">
    <item name="Ac"/>
  </item>
</articles>

生成所需的正确结果:

A-Aa-Aaa
A-Aa-Baa
A-Ba-Aba
A-Ba-Bba
B-Ab-Aab
B-Ab-Bab
B-Bb-Abb
B-Bb-Bbb
C-Ac
D

请注意:这是一个通用解决方案,适用于item元素的任何嵌套。

解释

  1. 我们执行两遍转换。

  2. 第一次通过的结果是:

    <x xmlns:ext="http://exslt.org/common">B-Bb-Bbb</x>
    <x xmlns:ext="http://exslt.org/common">B-Bb-Abb</x>
    <x xmlns:ext="http://exslt.org/common">B-Ab-Bab</x>
    <x xmlns:ext="http://exslt.org/common">B-Ab-Aab</x>
    <x xmlns:ext="http://exslt.org/common">A-Ba-Bba</x>
    <x xmlns:ext="http://exslt.org/common">A-Ba-Aba</x>
    <x xmlns:ext="http://exslt.org/common">A-Aa-Baa</x>
    <x xmlns:ext="http://exslt.org/common">A-Aa-Aaa</x>
    <x xmlns:ext="http://exslt.org/common">D</x>
    <x xmlns:ext="http://exslt.org/common">C-Ac</x>
    
  3. 第一次传递的处理使用一个仅与leafitem元素匹配的模板。对于每个叶item元素,其以短划线分隔的完整路径将作为名为x的元素的内容输出。

  4. 第二遍只是对第一遍的结果进行排序


II。XSLT2.0解决方案:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
 <xsl:template match="/">
     <xsl:for-each-group select="//item[not(item)]"
     group-by="string-join(ancestor-or-self::item/@name, '-')">
      <xsl:sort select="current-grouping-key()"/>
      <xsl:sequence select="current-grouping-key(), '&#xA;'"/>
     </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>

虽然不漂亮,但以下内容对3个项目的深度进行了排序,并且每个附加级别都需要额外的排序。我相信一个大师能够更一般地做到这一点:)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="articles">
        <xsl:apply-templates select="//item[not(item)]">
            <xsl:sort select="ancestor-or-self::item[parent::articles]/@name"/>
            <xsl:sort select="ancestor-or-self::item[parent::item[parent::articles]]/@name"/>
            <xsl:sort select="ancestor-or-self::item[parent::item[parent::item[parent::articles]]]/@name"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="item">
        <xsl:for-each select="ancestor-or-self::item">
            <xsl:value-of select="@name"/>
            <xsl:choose>
                <xsl:when test="not(item)">
                    <xsl:text>&#xA;</xsl:text>
                </xsl:when>
                <xsl:otherwise>-</xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

最新更新