0

私はxmlとMapオブジェクトを持っています.mapにはxmlノードに関する追加情報が含まれています。

<searchPersonResponse>
 <persons>
   <person>
     <id>123</id>
   </person>
   <person>
     <id>456</id>
   </person>
  </persons>
</searchPersonResponse>

そして私の地図はこのようなものです -

infoMap<123, <name="abc"><age="25">>
infoMap<456, <name="xyz"><age="80">>

そして、私が望む出力は次のようなものです:-

<searchPersonResponse>
 <persons>
  <person>
   <id>123</id>
   <name>abc</name>
   <age>25</age>
  </person>
  <person>
    <id>456</id>
    <name>xyz</name>
    <age>80</age>
   </person>
 </persons>
</searchPersonResponse>

この種のサンプル/サンプルをよく検索しましたが、同様のものは見つかりませんでした。助けてください !!前もって感謝します

4

1 に答える 1

0

すべてを同じ XML ファイルに入れることができるため、マップ情報とデータを含む次の XML ファイルを作成できます。

<xml>
    <!-- Original XML -->
    <searchPersonResponse>
        <persons>
            <person>
                <id>123</id>
            </person>
            <person>
                <id>456</id>
            </person>
        </persons>
    </searchPersonResponse>
    <!-- Map definition -->
    <map>
        <value id="123">
            <name>abc</name>
            <age>25</age>
        </value>
        <value id="456">
            <name>xyz</name>
            <age>80</age>
        </value>
    </map>
</xml>

これで、このスタイルシートを使用して、XML ファイルを目的の出力に変換できます。

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

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

    <!-- Identity template : copy all elements and attributes by default -->
    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="*|@*" />
        </xsl:copy>
    </xsl:template>

    <!-- Avoid copying the root element -->
    <xsl:template match="xml">
        <xsl:apply-templates select="searchPersonResponse" />
    </xsl:template>

    <xsl:template match="person">
        <!-- Copy the person node -->
        <xsl:copy>
            <!-- Save id into a variable to avoid losing the reference -->
            <xsl:variable name="id" select="id" />
            <!-- Copy attributes and children from the person element and the elements from the map-->
            <xsl:copy-of select="@*|*|/xml/map/value[@id = $id]/*" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
于 2013-02-19T10:39:48.063 に答える