本文介绍了如何使用 xslt 1.0 键计算而不是静态节点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过这个小的 XSL 转换,我可以给一个扁平"的 xml 结构提供一点层次结构:

With this small XSL transformation, I can give a "flat" xml structure a little bit of hierarchy:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="child1" match="item[meta/para/level]" use="generate-id(preceding-sibling::item[meta/para/level &lt; current()/meta/para/level][1])"/>
    <xsl:key name="child2" match="item[meta/text]" use="generate-id(preceding-sibling::item[meta/para/level][1])"/>
    <xsl:template match="/doc">
        <xsl:variable name="min"> <!-- xslt2.0 min() -->
            <xsl:for-each select="item/meta/para/level">
                <xsl:sort select="." data-type="number"/>
                <xsl:if test="position() = 1">
                    <xsl:value-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <root>
            <xsl:apply-templates select="item[meta/para/level = $min] | key('child2', '')"/>
        </root>
    </xsl:template>
    <xsl:template match="item[meta/para/level]">
        <test title="{meta/para/title}">
            <xsl:apply-templates select="key('child1', generate-id()) | key('child2', generate-id())"/>
        </test>
    </xsl:template>
    <xsl:template match="item[meta/text]">
        <text>
            <xsl:value-of select="meta/text"/>
        </text>
    </xsl:template>
</xsl:stylesheet>

应用于这个具有扁平结构的 XML 示例

Applied to this XML example with a flat structure

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <item>
        <meta>
            <text>abc</text>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>1</level>
                <title>a</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>2</level>
                <title>b</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>3</level>
                <title>c</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>4</level>
                <title>d</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>4</level>
                <title>e</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>5</level>
                <title>f</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>2</level>
                <title>g</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <text>def</text>
        </meta>
    </item>
    <item>
        <meta>
            <text>ghi</text>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>4</level>
                <title>h</title>
            </para>
        </meta>
    </item>
</doc>

是(期望的)分层结果如下:

Is the (desired) hierarchical result as follows:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <text>abc</text>
   <test title="a">
      <test title="b">
         <test title="c">
            <test title="d"/>
            <test title="e">
               <test title="f"/>
            </test>
         </test>
      </test>
      <test title="g">
         <text>def</text>
         <text>ghi</text>
         <test title="h"/>
      </test>
   </test>
</root>

如果级别"信息不是由整数决定的,而是由字符串的长度决定的,则转换不成功.


If the "level" information is not determined by an integer, but by the length of a string, the transformation does not succeed.

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <item>
        <meta>
            <text>abc</text>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>a</level>
                <title>a</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>bc</level>
                <title>b</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>def</level>
                <title>c</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>ghij</level>
                <title>d</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>klmn</level>
                <title>e</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>opqrs</level>
                <title>f</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>tu</level>
                <title>g</title>
            </para>
        </meta>
    </item>
    <item>
        <meta>
            <text>def</text>
        </meta>
    </item>
    <item>
        <meta>
            <text>ghi</text>
        </meta>
    </item>
    <item>
        <meta>
            <para>
                <level>vwxy</level>
                <title>h</title>
            </para>
        </meta>
    </item>
</doc>

通过调用string-length"找到我扩展的最小值的函数:

The function for finding the minimum I have extended by calling "string-length":

<xsl:sort select="string-length(.)" data-type="number"/>

应该如何使用键来将字符串的长度评估为级别信息?

推荐答案

@Vebbie 几乎正确(谢谢!).生成密钥时,排除不合适的节点很重要:<xsl:key name="child1" match="item[meta/para]"use="generate-id(preceding-sibling::item[meta/para and (string-length(meta/para/level) &lt; string-length(current()/meta/para/level))][1])"/> 所以完整的答案是

@Vebbie got it almost right (thank you!).When generating the key it's important to exclude non suitable nodes: <xsl:key name="child1" match="item[meta/para]" use="generate-id(preceding-sibling::item[meta/para and (string-length(meta/para/level) &lt; string-length(current()/meta/para/level))][1])"/> so the complete answer is

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="child1" match="item[meta/para]"
use="generate-id(preceding-sibling::item[meta/para and (string-length(meta/para/level) &lt; string-length(current()/meta/para/level))][1])"/>
    <xsl:key name="child2" match="item[meta/text]"
use="generate-id(preceding-sibling::item[meta/para][1])"/>
    <xsl:template match="/doc">
        <xsl:variable name="min">
            <!-- xslt2.0 min() -->
            <xsl:for-each select="item/meta/para/level">
                <xsl:sort select="string-length(.)" data-type="number"/>
                <xsl:if test="position() = 1">
                    <xsl:value-of select="string-length(.)"/>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <root>
            <xsl:apply-templates select="item[string-length(meta/para/level) = $min] | key('child2', '')"/>
        </root>
    </xsl:template>
    <xsl:template match="item[meta/para]">
        <test title="{meta/para/title}">
            <xsl:apply-templates select="key('child1', generate-id()) | key('child2', generate-id())"/>
        </test>
    </xsl:template>
    <xsl:template match="item[meta/text]">
        <text>
            <xsl:value-of select="meta/text"/>
        </text>
    </xsl:template>
</xsl:stylesheet>

这篇关于如何使用 xslt 1.0 键计算而不是静态节点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:04