Xml 如何使用XSLT来排列节点层次结构,第2部分

Xml 如何使用XSLT来排列节点层次结构,第2部分,xml,xslt,Xml,Xslt,这个问题扩大了一个范围 在前面的问题中,我问了如何在固定深度的树中排列节点层次结构。例如,对于路径为/x/y/z的每个叶,我希望输出中的叶具有路径y/x/z。(排列2,1,3) 我现在要做的不是处理固定长度的置换,而是像“2,1,3..n-1”这样的置换 因此,我的XSLT现在看起来如下所示: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output

这个问题扩大了一个范围

在前面的问题中,我问了如何在固定深度的树中排列节点层次结构。例如,对于路径为/x/y/z的每个叶,我希望输出中的叶具有路径y/x/z。(排列2,1,3)

我现在要做的不是处理固定长度的置换,而是像“2,1,3..n-1”这样的置换

因此,我的XSLT现在看起来如下所示:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/input">
    <output>
        <xsl:apply-templates select="text()"/>
    </output>
</xsl:template>

    <!-- example 2,1,3:n -->

    <xsl:template match="text()">
        <xsl:variable name="n" select="5" /> <!-- this should later be the depth of the leaf -->
        <xsl:element name="{name(ancestor::*[2])}"> <!-- 2 -->
            <xsl:element name="{name(ancestor::*[1])}"> <!-- 1 -->
                <xsl:call-template name="loop"> <!-- 3:5 -->
                    <xsl:with-param name="i" select="3" />
                    <xsl:with-param name="end" select="$n - 1" />
                </xsl:call-template>
            </xsl:element>
        </xsl:element>
    </xsl:template>


    <xsl:template name="loop">
        <xsl:param name="i" />
        <xsl:param name="end" />

        <xsl:choose>
            <xsl:when test="$i = $end">
                    <xsl:copy-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{name(ancestor::*[$i])}">
                    <!-- recursive call -->
                    <xsl:call-template name="loop">
                        <xsl:with-param name="i" select="$i+1" />
                        <xsl:with-param name="end" select="$end" />
                    </xsl:call-template>

                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>


我甚至不确定这种递归使用模板在理论上是否可行。我走对了吗?

我希望下面的样式表能够解决这个问题:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <!-- template to match the root element -->
    <xsl:template match="/input">
        <output>
            <!-- looping on text nodes having more than 3 ancestors -->
            <xsl:for-each select="//text()[count(ancestor::*) > 3]">
                <xsl:apply-templates select="." mode="copy-ancestors">
                    <xsl:with-param name="doc" select="."/>
                </xsl:apply-templates>
            </xsl:for-each>
        </output>
    </xsl:template>

    <xsl:template match="node()" mode="copy-ancestors">
        <!-- param doc to contain the document generated while traversing in the reverse direction -->
        <xsl:param name="doc"/>

        <!-- generate temporary document by adding immidiate ancestor's name-->
        <xsl:variable name="tempDoc">
            <xsl:choose>
                <xsl:when test="not(self::*)">
                    <xsl:copy-of select="."/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="{name(ancestor::*[1])}">
                        <xsl:copy-of select="exsl:node-set($doc)/node()"/>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:choose>
            <!-- the base condition -->
            <xsl:when test="count(ancestor::*) = 3">
                <xsl:element name="{name(ancestor::*[1])}">
                    <xsl:element name="{name(ancestor::*[2])}">
                        <xsl:copy-of select="exsl:node-set($doc)/*"/>
                    </xsl:element>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="ancestor::*[1]" mode="copy-ancestors">
                    <xsl:with-param name="doc" select="exsl:node-set($tempDoc)"/>
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

我相信真正的通用解决方案应该是这样的:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/input">
    <output>
        <xsl:apply-templates select="text()"/>
    </output>
</xsl:template>

    <!-- example 2,1,3:n -->

    <xsl:template match="text()">
        <xsl:variable name="n" select="5" /> <!-- this should later be the depth of the leaf -->
        <xsl:element name="{name(ancestor::*[2])}"> <!-- 2 -->
            <xsl:element name="{name(ancestor::*[1])}"> <!-- 1 -->
                <xsl:call-template name="loop"> <!-- 3:5 -->
                    <xsl:with-param name="i" select="3" />
                    <xsl:with-param name="end" select="$n - 1" />
                </xsl:call-template>
            </xsl:element>
        </xsl:element>
    </xsl:template>


    <xsl:template name="loop">
        <xsl:param name="i" />
        <xsl:param name="end" />

        <xsl:choose>
            <xsl:when test="$i = $end">
                    <xsl:copy-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{name(ancestor::*[$i])}">
                    <!-- recursive call -->
                    <xsl:call-template name="loop">
                        <xsl:with-param name="i" select="$i+1" />
                        <xsl:with-param name="end" select="$end" />
                    </xsl:call-template>

                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/*">
    <output>
        <xsl:apply-templates select="//text()"/>
    </output>
</xsl:template>

<xsl:template match="text()">
    <xsl:call-template name="re-order">
        <xsl:with-param name="node-set" select="ancestor::*|." />
        <xsl:with-param name="order">2,1,3,</xsl:with-param>
    </xsl:call-template>
</xsl:template>


<xsl:template name="re-order">
    <xsl:param name="node-set" />
    <xsl:param name="order" />
    <xsl:param name="i" select="1" />

    <xsl:variable name="n" select="count($node-set)" />
    <xsl:variable name="p">
        <xsl:choose>
            <xsl:when test="$order">
                <xsl:value-of select="substring-before($order, ',')" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$i" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:choose>
        <xsl:when test="$i &lt; $n">
            <xsl:element name="{name($node-set[number($p)])}">
                <!-- recursive call -->
                <xsl:call-template name="re-order">
                    <xsl:with-param name="node-set" select="$node-set" />
                    <xsl:with-param name="order" select="substring-after($order, ',')" />
                    <xsl:with-param name="i" select="$i + 1" />
                </xsl:call-template>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$node-set[last()]"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

2,1,3,
测试输入

<one>
    <two>
        <three>3.1</three>
        <three>3.2</three>
    </two>
    <two>
        <three>
            <four>4.1</four>
            <four>4.2</four>
        </three>
        <three>
            <four>
                <five>5.1</five>
                <five>5.2</five>
            </four>
            <four>
                <five>5.3</five>
                <five>5.4</five>
            </four>
        </three>
    </two>
    <two>
         <three>
            <four>
                <five>
                    <six>6.1</six>
                    <six>6.2</six>
                </five>
            </four>
        </three>
    </two>
    <two>2.1</two>
    <two>2.2</two>
</one>

3.1
3.2
4.1
4.2
5.1
5.2
5.3
5.4
6.1
6.2
2.1
2.2
结果:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <two>
      <one>
         <three>3.1</three>
      </one>
   </two>
   <two>
      <one>
         <three>3.2</three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>4.1</four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>4.2</four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>
               <five>5.1</five>
            </four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>
               <five>5.2</five>
            </four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>
               <five>5.3</five>
            </four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>
               <five>5.4</five>
            </four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>
               <five>
                  <six>6.1</six>
               </five>
            </four>
         </three>
      </one>
   </two>
   <two>
      <one>
         <three>
            <four>
               <five>
                  <six>6.2</six>
               </five>
            </four>
         </three>
      </one>
   </two>
   <two>
      <one>2.1</one>
   </two>
   <two>
      <one>2.2</one>
   </two>
</output>

3.1
3.2
4.1
4.2
5.1
5.2
5.3
5.4
6.1
6.2
2.1
2.2
注释

<one>
    <two>
        <three>3.1</three>
        <three>3.2</three>
    </two>
    <two>
        <three>
            <four>4.1</four>
            <four>4.2</four>
        </three>
        <three>
            <four>
                <five>5.1</five>
                <five>5.2</five>
            </four>
            <four>
                <five>5.3</five>
                <five>5.4</five>
            </four>
        </three>
    </two>
    <two>
         <three>
            <four>
                <five>
                    <six>6.1</six>
                    <six>6.2</six>
                </five>
            </four>
        </three>
    </two>
    <two>2.1</two>
    <two>2.2</two>
</one>
  • 默认情况下,根元素作为最顶层的祖先包含 叶文本节点(您可以在调用模板时覆盖此节点) 第一次)
  • 假设任何叶文本节点的深度不小于
    $order
    参数中的令牌数;否则你可能会 意外的结果