2

XML ドキュメントを、値が前の兄弟の属性に基づくリストに変換しようとしています。

サンプル XML:

<myRoot>
    <Person>Craig</Person>
    <Person rank="10">Woody</Person>
    <Person>Brian</Person>
    <Person>Michael</Person>    
    <Person rank="20">Emily</Person>
    <Person>Chris</Person>
</myRoot>

私が欲しいもの:

<myNewRoot>
    <Index>1: Craig</Index>
    <Index>10: Woody</Index>
    <Index>11: Brian</Index>
    <Index>12: Michael</Index>
    <Index>20: Emily</Index>
    <Index>21: Chris</Index>
</myNewRoot>

@rank 属性を持つ最後の先行兄弟と現在のノードとの間の距離を特定できず、行き詰まりました。

ここに私の現在のスタイルシートがあります

<xsl:template match="Person">
    <xsl:element name="Index">
        <xsl:choose>

            <xsl:when test="./@rank">
                <xsl:value-of select="./@rank"/>
            </xsl:when>


            <xsl:when test="preceding-sibling::Person[@rank]">
                <xsl:value-of select="count(.|preceding-sibling::*[. &gt; current()/preceding-sibling::Person[@rank][1]])   +       preceding-sibling::Person[@rank][1]/@rank"/>

            </xsl:when>


            <xsl:otherwise>
                <xsl:value-of select="position()"/>
            </xsl:otherwise>

        </xsl:choose>

        <xsl:text>: </xsl:text>
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

count() 機能を動作させることができず、結局は

<myNewRoot>
    <Index>1: Craig</Index>
    <Index>10: Woody</Index>
    <Index>11: Brian</Index>
    <Index>11: Michael</Index>
    <Index>20: Emily</Index>
    <Index>21: Chris</Index>
</myNewRoot>
4

1 に答える 1

2

解決策は、再帰テンプレートを使用して、テンプレートにパラメーターとして渡される、出力された前の値に基づいて現在の値を取得することです。

<xsl:output method="xml" indent="yes" />

<xsl:template match="myRoot">
    <myNewRoot>
        <xsl:call-template name="make-index" />
    </myNewRoot>
</xsl:template>

<xsl:template name="make-index">
    <xsl:param name="element" select="Person" />
    <xsl:param name="count" select="'0'" />

    <!-- Continue if there is some element left -->
    <xsl:if test="$element">
        <!-- Obtain number to be printed next -->
        <xsl:variable name="next-rank">
            <xsl:choose>
                <!-- If the rank attribute is present, output its value -->
                <xsl:when test="$element[1]/@rank">
                    <xsl:value-of select="$element[1]/@rank" />
                </xsl:when>
                <!-- If the rank attribute is not present, increase the previous
                     printed value by one -->
                <xsl:otherwise>
                    <xsl:value-of select="$count + 1" />
                </xsl:otherwise>                    
            </xsl:choose>
        </xsl:variable>
        <!-- Output the index along with the value of the current index -->
        <Index>
            <xsl:value-of select="concat($next-rank, ': ',  $element[1])" />
        </Index>
        <!-- Recurse until we do not have any element left -->
        <xsl:call-template name="make-index">
            <xsl:with-param name="element" select="$element[position() > 1]" />
            <xsl:with-param name="count" select="$next-rank" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>


アップデート。次の解決策は再帰に依存せず、おそらく前のものほど効率的ではありませんが (これにはより複雑な XPath 操作があります)、より短く、前のものとは異なるアプローチである兄弟のグループ化に依存しています。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="myRoot">
        <myNewRoot>
            <!-- Print all the elements before the first element with a rank
                 attribute defined -->
            <xsl:apply-templates select="Person[(preceding-sibling::Person[@rank])][not(@rank)]" mode="print" />
            <!-- Match all the elements with a rank attribute defined -->
            <xsl:apply-templates select="Person[@rank]" />
        </myNewRoot>
    </xsl:template>

    <!-- Match the set of Person elements with @rank defined -->
    <xsl:template match="Person[@rank]">
        <!-- Obtain id of current node before losing the context -->
        <xsl:variable name="id" select="generate-id()" />
        <!-- Match the current node along all the following siblings without @rank such
             as their nearest preceding-sibling with @rank defined is the current
             element, i.e all the elements between the current element and the next
             element with @rank defined -->
        <xsl:apply-templates select=".|following-sibling::Person[not(@rank)][generate-id(preceding-sibling::Person[@rank][1]) = $id]" mode="print">
            <xsl:with-param name="rank" select="@rank" />
        </xsl:apply-templates>
    </xsl:template>

    <!-- Print the information from a Person node, using rank to determine
         the position -->
    <xsl:template match="Person" mode="print">
        <xsl:param name="rank" select="'1'" />
        <Index>
            <xsl:value-of select="concat($rank + position() - 1, ': ', .)" />
        </Index>
    </xsl:template>


注: XSLT 1.0 を使用している両方のソリューションを想定しています。XSLT 2.0 を使用している場合、ソリューションは以前のものよりも簡単になります。

于 2013-02-21T19:36:57.107 に答える