-2

次の xsl スクリプトがあります。これは、2 つの xml ファイルをフィールドで 1 つのファイルに結合できます。

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

  <xsl:key name="trans" match="Transaction" use="id" />

  <!-- Identity template to copy everything we don't specifically override -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <!-- override for Mail elements -->
  <xsl:template match="Mail">
    <xsl:copy>
      <!-- copy all children as normal -->
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="myId" select="id" />
      <Transaction_data>
        <xsl:for-each select="document('transactions.xml')">
          <!-- process all transactions with the right ID -->
          <xsl:apply-templates select="key('trans', $myId)" />
        </xsl:for-each>
      </Transaction_data>
    </xsl:copy>
  </xsl:template>

  <!-- omit the id element when copying a Transaction -->
  <xsl:template match="Transaction/id" />

同じ結合ノードで任意の数のxmlファイルで同じ処理を行いたいです。単一の xsl ファイルで何とか可能ですか?

4

1 に答える 1

2

任意の数の入力ファイルを処理したい場合は、ファイル名をパラメーターとして XML ドキュメントを渡すことを検討してください。たとえばfiles-to-process、内容が似ているファイルをパラメーターとして渡します。

<files>
  <file>foo.xml</file>
  <file>bar.xml</file>
  <file>baz.xml</file>
</files>

それから持っている

<xsl:param name="files-url" select="'files-to-process.xml'"/>
<xsl:variable name="files-doc" select="document($files-url)"/>

そして、単に変更します

  <xsl:template match="Mail">
    <xsl:copy>
      <!-- copy all children as normal -->
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="myId" select="id" />
      <Transaction_data>
        <xsl:for-each select="document('transactions.xml')">
          <!-- process all transactions with the right ID -->
          <xsl:apply-templates select="key('trans', $myId)" />
        </xsl:for-each>
      </Transaction_data>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Mail">
    <xsl:copy>
      <!-- copy all children as normal -->
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="myId" select="id" />
      <Transaction_data>
        <xsl:for-each select="document($files-doc/files/file)">
          <!-- process all transactions with the right ID -->
          <xsl:apply-templates select="key('trans', $myId)" />
        </xsl:for-each>
      </Transaction_data>
    </xsl:copy>
  </xsl:template>

files/fileそうすれば、パラメータとして渡されたドキュメントで指定されたすべてのファイルを処理できます。

于 2013-01-03T14:15:27.140 に答える