1

私は XSL にまったく慣れていないので、いくつかのことを学ぼうとしています。次のような XML があるとします。

<?xml version="1.0" encoding="UTF-8"?>
<Result>
    <Node>
        <node-id>1</node-id>
        <node-path>2,3</node-path>
    </Node>
    <Node>
        <node-id>2</node-id>
        <node-path>2,3,4</node-path>
    </Node>
    <Node>
        <node-id>3</node-id>
        <node-path>123,34</node-path>
    </Node>
    <Node>
        <node-id>4</node-id>
        <node-path>2124,14,14</node-path>
    </Node>
    <Node>
        <node-id>5</node-id>
        <node-path>1,0</node-path>
    </Node>
</Result>

そして、次のように node-path フィールドに 2 つの値しかないすべてのノードを取得したいと考えています。

<?xml version="1.0" encoding="UTF-8"?>
    <Result>
    <Node>
        <node-id>1</node-id>
        <node-path>2,3</node-path>
    </Node>
    <Node>
        <node-id>3</node-id>
        <node-path>123,34</node-path>
    </Node>
    <Node>
        <node-id>5</node-id>
        <node-path>1,0</node-path>
    </Node>
</Result>

XSLでこれを行うにはどうすればよいですか? ノードをコピーする必要があるため、恒等変換をテンプレートとして使用する必要があることがわかりました。また、再帰を使用してトークンをカウントする必要があることもわかりました。私はこれを思いつきました:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:template name="root-nodes">
        <xsl:for-each select="/Result/Node">
            <xsl:variable name="path" select="node-path" />
            <xsl:call-template name="tokenizer" mode="matcher">
                <xsl:with-param name="list" select="$path" />
                <xsl:with-param name="delimiter" select="','" />
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Could not figure out how to write this recursion -->
    <xsl:template name="tokenizer" mode="matcher">
        <xsl:param name="list"/>
        <xsl:param name="delimiter" />
        <xsl:value-of select="substring-before($list,$delimiter)" />
        <xsl:call-template name="tokenizer">
            <xsl:with-param name="list" select="substring-after($list,$delimiter)" />
            <xsl:with-param name="delimiter" select="','" />
        </xsl:call-template>
    </xsl:template>

</xsl:stylesheet>

しかし、再帰部分に問題があります。トークンをカウントし、カウントが 2 の場合にのみ恒等変換を行うようにするにはどうすればよいですか? 再帰テンプレートを修正するにはどうすればよいですか? 既存の「トークナイザー」テンプレートの問題は何ですか (トークンを提供していません)? 追加のリソース/リンクは非常に優れています。

4

2 に答える 2