5

XSLTを作成しましたが、1つのタグセット間ですべてのノードをコピーし、下部に別のタグを追加する方法を考えていました。追加するタグとその名前を決定するためのすべてのロジックを備えたXSLTを作成しました。ただし、現在発生している問題は、他のすべてのタグもコピーできないことです。問題のファイルは次のとおりです。

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="/csvImportSchema">
        <csvImportSchema>
            <xsl:for-each select="payload">
                <payload>
                    <xsl:copy-of select="@*"/>
                    <xsl:variable name="ean">
                        <xsl:value-of select="ean"/>
                    </xsl:variable>
                    <xsl:for-each select="../product">
                        <xsl:if test="ean = $ean">
                            <productId><xsl:value-of select="article"/></productId>
                        </xsl:if>
                    </xsl:for-each>
                </payload>
            </xsl:for-each>
        </csvImportSchema>
    </xsl:template>

</xsl:stylesheet>

入力

<?xml version="1.0" encoding="UTF-8"?>
<csvImportSchema>
    <payload>
        <test>1</test>
        <test2>2</test2>
        <test3>3</test3>
        <ean>1111111111</ean>
        <productId/>
    </payload>
    <product>
        <article>722619</article>
        <ean>1111111111</ean>
    </product>
</csvImportSchema>

電流出力

<?xml version="1.0" encoding="utf-8"?>
<csvImportSchema>
    <payload>
        <productId>722619</productId>
    </payload>
</csvImportSchema>

必要な出力

<?xml version="1.0" encoding="UTF-8"?>
<csvImportSchema>
    <payload>
        <test>1</test>
        <test2>2</test2>
        <test3>3</test3>
        <ean>1111111111</ean>
        <productId>722619</productId>
    </payload>
</csvImportSchema>
4

4 に答える 4

9

この短くて単純な変換

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="productId">
  <productId>
    <xsl:value-of select="../../product/article"/>
  </productId>
 </xsl:template>
 <xsl:template match="product"/>
</xsl:stylesheet>

提供されたXMLドキュメントに適用した場合:

<csvImportSchema>
    <payload>
        <test>1</test>
        <test2>2</test2>
        <test3>3</test3>
        <ean>1111111111</ean>
        <productId/>
    </payload>
    <product>
        <article>722619</article>
        <ean>1111111111</ean>
    </product>
</csvImportSchema>

必要な正しい結果を生成します

<csvImportSchema>
   <payload>
      <test>1</test>
      <test2>2</test2>
      <test3>3</test3>
      <ean>1111111111</ean>
      <productId>722619</productId>
   </payload>
</csvImportSchema>

説明

  1. IDルールは、実行対象として選択されたすべてのノードを「現状のまま」コピーします。

  2. 一致するオーバーライドテンプレートproductは、この要素を出力から(空の本文によって)「削除」します。

  3. 別のオーバーライドテンプレートproductIdは、から取得したテキストノードの子と一致してこの要素を生成しますproduct/article

于 2012-09-18T12:36:50.373 に答える
2

あなたのコードに関する1つの観察。これを使用しないでください:

<xsl:variable name="ean">
    <xsl:value-of select="../ean"/>
</xsl:variable>

あなたがこれを書くことができるとき:

<xsl:variable name="ean" select="../ean"/>

冗長であるだけでなく、非常に非効率的です。$ eanを既存のノードにバインドする代わりに、既存のノードの文字列値を抽出し、その文字列値でテキストノードを形成し、新しいXMLドキュメントツリーを作成し、追加します。この新しいドキュメントのコンテンツへのこのテキストノード。(私はかつて、この恐ろしい構成を排除することにより、3倍速く実行するスタイルシートを手に入れました。)

于 2012-09-18T20:52:56.180 に答える
1

xsl:copy-of select="@*"/ペイロードを次のように変更するのと同じくらい簡単なはずです

<xsl:copy-of select="*[local-name() != 'productId'] | @*"/>

つまりproductId、これを手動で作成するため、を除くすべてをコピーします。

これにより、必要な出力が得られます

<?xml version="1.0" encoding="utf-8"?>
<csvImportSchema>
  <payload>
    <test>1</test>
    <test2>2</test2>
    <test3>3</test3>
    <ean>1111111111</ean>
    <productId>722619</productId>
  </payload>
</csvImportSchema>
于 2012-09-18T09:31:12.050 に答える
1

このXSLTはその役割を果たし、より多くのCOPYタグとテンプレートを使用する必要があります。たぶん、1つのxsl:templateですべてを実行するわけではありません(私の意見)。

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="/csvImportSchema">
        <xsl:copy>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="csvImportSchema/payload/productId">
        <xsl:variable name="ean">
            <xsl:value-of select="../ean"/>
        </xsl:variable>
        <xsl:for-each select="../../product">
            <xsl:if test="ean = $ean">
                <productId><xsl:value-of select="article"/></productId>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="csvImportSchema/product">
        <!-- do not copy -->
    </xsl:template>

    <xsl:template match="csvImportSchema/payload">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>   
</xsl:stylesheet>
于 2012-09-18T09:33:23.623 に答える