如何使用 XSLT 来排列节点层次结构,第 2 部分 [英] How can I use XSLT to permutate a node hierarchy, part 2
问题描述
这个问题扩展了上一个问题.
在上一个问题中,我询问了如何在固定深度的树中排列节点层次结构.例如,对于路径为/x/y/z 的每个叶子,我希望输出中的叶子具有路径 y/x/z.(排列 2,1,3).
In the previous question I asked how to permutate the node hierarchy in a tree of fixed depth. For example for every leaf with the path /x/y/z I wanted the leaf in the output to have the path y/x/z. (permutation 2,1,3).
我现在想做的不是处理固定长度的排列,而是像 "2,1, 3..n-1" 这样的排列
Instead of handling fixed-length permutations, what I want to do now are permutations like "2,1, 3..n-1"
所以我的 XSLT 现在看起来像这样:
So my XSLT now looks like this:
<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>
我什至不确定这种递归使用模板在理论上是否可行.我在正确的轨道上吗?
I'm not even sure if this recursive use of templates could work theoretically. Am I on the right track?
推荐答案
我相信一个真正通用的解决方案应该是这样的:
I believe a truly generic solution would look something like this:
XSLT 1.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 < $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>
测试输入:
<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>
结果:
<?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>
注意事项:
- 默认情况下,根元素被包含为最顶层的祖先元素叶文本节点(您可以在调用模板时覆盖它第一次);
- 假设任意叶子文本节点的深度不小于
$order
参数中的标记数;否则你可能会得到意想不到的结果.
- By default, the root element is included as the top-most ancestor of the leaf text node (you can override this when calling the template for the fist time);
- It is assumed that the depth of any leaf text node is no less than
the number of tokens in the
$order
parameter; otherwise you may get unexpected results.
这篇关于如何使用 XSLT 来排列节点层次结构,第 2 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!