2

変換する xml データの次の構造があります。

 <root>
    <main1>
            <text-body>
                <title>A</title>
                <subtitle>A</subtitle>
            </text-body>
    <!-- other stuff -->
            <text-body>
                <titel>Aa</titel>
                <subtitel>Aa</subtitel>
            </text-body>
    <!-- other stuff -->
            <text-body>
                <titel>Aaa</titel>
                <subtitel>Aaa</subtitel>
            </text-body>
    </main1>
    <main2>
        <text-body>
            <title>B</title>
            <subtitle>B</subtitle>
            <body>B</body>
        </text-body>
        <text-body>
            <title>C</title>
            <subtitle>C</subtitle>
            <body>C</body>
        </text-body>
        <text-body>
            <title>D</title>
            <subtitle>D</subtitle>
            <body>D</body>
        </text-body>
    </main2>
    </root>

また、main/text-body のデータを main2/text-body のデータに置き換える必要がありますが、その他のものは main1 に保持します。出力は次のようになります。

<root>
        <main1>
              <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
                </text-body>
        <!-- other stuff -->
              <text-body>
                 <title>C</title>
                <subtitle>C</subtitle>
                <body>C</body>
              </text-body>
        <!-- other stuff -->
              <text-body>
                <title>D</title>
                <subtitle>D</subtitle>
                <body>D</body>
              </text-body>
        </main1>
        <main2>
            <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
            </text-body>
            <text-body>
                <title>C</title>
                <subtitle>C</subtitle>
                <body>C</body>
            </text-body>
            <text-body>
                <title>D</title>
                <subtitle>D</subtitle>
                <body>D</body>
            </text-body>
        </main2>
        </root>

次の xsl コードがあります。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml"/>

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

    <xsl:template match="main1/text-body">
           <xsl:param name="count" select="count(preceding-sibling::node())"/>
           <xsl:copy-of select="/root/main2/text-body[$count]"/>
    </xsl:template>

</xsl:stylesheet>

main1/text-body の現在のノード インデックスを取得して、main2 の右側の text-body を埋めようとしています。しかし、うまくいきません。現在の出力は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?><root>
        <main1>
              <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
            </text-body>
        <!-- other stuff -->

        <!-- other stuff -->

        </main1>
        <main2>
            <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
            </text-body>
            <text-body>
                <title>C</title>
                <subtitle>C</subtitle>
                <body>C</body>
            </text-body>
            <text-body>
                <title>D</title>
                <subtitle>D</subtitle>
                <body>D</body>
            </text-body>
        </main2>
        </root>

助けてください!

4

1 に答える 1

6

XPathとXSLTは1ベースのインデックスです。最初の項目を選択するために、述語フィルターはで[1]はなく、を検索します[0]

その場合、変数は前の兄弟の選択に追加されて$countいる必要があります。また、すべて/すべてではなく、先行する兄弟である要素を数える必要があります1count()text-bodynode()

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml"/>

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

    <xsl:template match="main1/text-body">
        <xsl:param name="count" select="count(preceding-sibling::text-body)+1"/>
        <xsl:copy-of select="/root/main2/text-body[$count]"/>
    </xsl:template>

</xsl:stylesheet>
于 2013-01-13T18:39:17.807 に答える