0

変換するxmlデータの構造は次のとおりです。

<root>
        <main1>
            <page>
                <text-body>
                    <title>K1</title>
                    <subtitle>Text</subtitle>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>K2</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>K3</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
        </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>

また、main1 / text-bodyのデータをタイトルK1、K2、K3のデータをmain2 / text-bodyのデータに置き換える必要がありますが、他のtext-body要素はmain1に残します。出力は次のようになります。

<root>
        <main1>
            <page>
                <text-body>
                    <title>B</title>
                    <subtitle>B</subtitle>
                    <body>B</body>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>C</title>
                    <subtitle>C</subtitle>
                    <body>C</body>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>D</title>
                    <subtitle>D</subtitle>
                    <body>D</body>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
        </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/page/text-body">
            <xsl:param name="count" select="title/substring(.,2,1)"/>
            <xsl:if test="title/substring(.,1,1)='K'">
               <xsl:copy-of select="/root/main2/text-body[$count]"/>
            </xsl:if>
        </xsl:template>

    </xsl:stylesheet>

タイトルの数字を選んで、本文にKが入っているか確認してみました。しかし、それは機能しません。そして、他のテキスト本文要素を保持する方法がわかりません。現在の出力は次のとおりです。

 <main1>
            <page>
                <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>


            </page>
            <page>
                <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>

            </page>
            <page>
                <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>

            </page>
        </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

2 に答える 2

0

述語を使用して、text-body本当に交換したい要素のみに一致させます。XSLT 2.0を使用しているため、述語で正規表現を使用できます。

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

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

  <xsl:template match="main1/page/text-body[matches(title,'^K\d+$')]">
    <xsl:variable name="count" select="xs:integer(title/substring(.,2))"/>
    <xsl:copy-of select="/root/main2/text-body[$count]"/>
  </xsl:template>

</xsl:stylesheet>
于 2013-01-16T22:38:01.147 に答える
0

主な問題は、カウントパラメーターが文字列に設定されていることですが、インデックスとして使用する場合は数値にする必要があるため、これを行う必要があります

<xsl:copy-of select="/root/main2/text-body[number($count)]"/>

また、現在のテンプレートでは、if 条件が false の場合にtext-body要素が出力されないため、ここでxsl:ifを使用したくないでしょう。実際には、テストをテンプレート マッチの一部に移動する必要があります。

<xsl:template match="main1/page/text-body[title/substring(.,1,1)='K']">

ここに完全な XSLT があります

<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/page/text-body[title/substring(.,1,1)='K']">
      <xsl:param name="count" select="title/substring(.,2,1)"/>
      <xsl:copy-of select="/root/main2/text-body[number($count)]"/>
   </xsl:template>
</xsl:stylesheet>

これにより、期待どおりの出力が生成されます。

別のアプローチに興味がある場合は、 xsl:keyを定義してmain2/text-body要素を検索することを選択できます

<xsl:key name="main2" 
     match="main2/text-body" 
     use="concat('K', count(preceding-sibling::text-body) + 1)" />

そうすれば、代わりに次のテンプレートを使用してmain1/page/text-body要素を一致させることで、部分文字列の必要性を取り除くことができます

 <xsl:template match="main1/page/text-body[key('main2', title)]">
    <xsl:copy-of select="key('main2', title)"/>
 </xsl:template>
于 2013-01-16T23:26:38.070 に答える