1

XSLT 2.0 変換では、2 つの入力があります。

最初のペイロードを入力してください:

<datas>
   <data1>1000</data1>
   <data2>2000</data2>
   <data3>1000</data3>
   <name1>1000</name1>
   <name2>1000</name2>
</datas>

2 番目の入力は、コード変換サービスの結果です

<results>
  <result>
   <type>data<type>
   <key>1000</key>
   <value>john</value>
  <result>
  <result>
   <type>data<type>
   <key>2000</key>
   <value>tom</value>
  <result>
  <result>
   <type>name<type>
   <key>1000</key>
   <value>marc</value>
  <result>
  <result>
   <type>data<type>
   <key>1000</key>
   <value>john</value>
  <result>     
  <result>
   <type>name<type>
   <key>1000</key>
   <value>marc</value>
  <result>
 </results>

出力として必要

<datas>
  <data1>john</data1>
  <data2>tom</data2>
  <data3>john</data3>
  <name1>marc</name1>
  <name2>marc</name2>
</datas>

毎回すべてのデータを実行せずに応答をマップする xslt テンプレートを作成したい。結果の順序は入力データと同じではありません

何か案が ?

よろしく

4

2 に答える 2

1

あなたができることは、最初のXMLドキュメントのデータ要素の子要素を最初に一致させることです

<xsl:template match="datas/*">

次に、要素の「タイプ」と「位置」を変数に抽出します

<xsl:variable name="type" select="substring(local-name(), 1, 4)" />
<xsl:variable name="position" select="number(substring(local-name(), 5))" />

最後に、次のように 2 番目のドキュメント (この例では「test2.xml」と呼んでいます) で関連する名前を検索できます。

<xsl:apply-templates select="document('test2.xml')/results/result[type=$type][position()=$position]/value/text()" />

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

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

    <xsl:template match="datas/*">
        <xsl:variable name="type" select="substring(local-name(), 1, 4)" />
        <xsl:variable name="position" select="number(substring(local-name(), 5))" />
        <xsl:copy>
           <xsl:apply-templates select="document('test2.xml')/results/result[type=$type][position()=$position]/value/text()" />
        </xsl:copy>
    </xsl:template>

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

入力 XML に適用すると、次のように出力されます。

<datas>
   <data1>john</data1>
   <data2>tom</data2>
   <data3>john</data3>
   <name1>marc</name1>
   <name2>marc</name2>
</datas>

編集: Martin Honnen がコメントで正しく指摘したように (Martin に感謝します!)、これはキーを使用する方が適切です。最初に次のようにキーを定義します。

 <xsl:key name="lookup" match="result" use="type" />

次に、次のように 2 番目のドキュメントからテキストを検索できます。

<xsl:apply-templates
     select="key('lookup', $type, document('test2.xml'))[$position]/value/text()" />

この XSLT も動作するはずです (XSLT 2.0 の場合)。

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

    <xsl:key name="lookup" match="result" use="type" />

    <xsl:template match="datas/*">
        <xsl:variable name="type" select="substring(local-name(), 1, 4)" />
        <xsl:variable name="position" select="number(substring(local-name(), 5))" />
        <xsl:copy>
           <xsl:apply-templates
                select="key('lookup', $type, document('test2.xml'))[$position]/value/text()" />
        </xsl:copy>
    </xsl:template>

     <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
于 2013-03-29T16:02:36.007 に答える