1

私のソースファイルは次のようになります。

<x>
<names>
         <name>name1</name>
         <name>name2</name>
</names>
<codes>
         <code>code1</code>
         <code>code2</code>
</codes>
<stuff> stuff </stuff>
</x>

そして、この出力を取得するために変換したいと思います。

<out>
<y>
    <name>name1</name>
    <code>code1</code>
    <stuff> stuff </stuff>
</y>
<y>
    <name>name2</name>
    <code>code2</code>
    <stuff> stuff </stuff>
</y>
</out>

ソースファイル内の名前とコードタグの数はわかりませんが、名前の数がコードの数と等しいことは知っています。

いくつかのヒント、それを行う方法を共有してください。

4

1 に答える 1

2

この変換

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

 <xsl:template match="/*">
  <out>
   <xsl:apply-templates select="names/name"/>
  </out>
 </xsl:template>

 <xsl:template match="name">
  <xsl:variable name="vPos" select="position()"/>
  <y>
   <xsl:copy-of select="."/>
   <xsl:copy-of select=
     "../../codes/code[position()=$vPos]"/>
   <xsl:copy-of select="/*/stuff"/>
  </y>
 </xsl:template>
</xsl:stylesheet>

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

<x>
    <names>
        <name>name1</name>
        <name>name2</name>
    </names>
    <codes>
        <code>code1</code>
        <code>code2</code>
    </codes>
    <stuff> stuff </stuff>
</x>

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

<out>
   <y>
      <name>name1</name>
      <code>code1</code>
      <stuff> stuff </stuff>
   </y>
   <y>
      <name>name2</name>
      <code>code2</code>
      <stuff> stuff </stuff>
   </y>
</out>
于 2010-12-20T19:59:16.303 に答える