使用一个XSLT转换多个XML文件

时间:2013-06-05 21:11:02

标签: xslt-2.0

我有以下xml文件,我尝试使用身份转换进行复制。 我想要做的是使用集合功能将所有源文件的所有内容合并到一个xml文件中。我还需要在所有和元素上使用generate-id()。 我已经阅读过这个论坛和其他地方,但是由于我仍在使用我的XSLT,因此我无法获得所需的文档。

我看过几个例子,但似乎没有一个能做我需要的事。

以下是我使用Saxon 9.4.0.6进行转换的样式表。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*" mode="inFile">
        <xsl:copy>
        <xsl:apply-templates mode="inFile" select="node()|@*"/>
                    </xsl:copy>
                </xsl:template>
    <xsl:template match="/">  

                <xsl:variable name="titles" select="collection('file:/c:/U/?select=*.dita;recurse=yes')//title"/>
                <xsl:for-each select="$titles">

                    <xsl:copy-of select="."/>
                </xsl:for-each>

        <xsl:variable name="parags" select="collection('file:/c:/U/?select=*.dita;recurse=yes')//p"/>
                <xsl:for-each select="$parags">

                <xsl:copy-of select="."/>
                </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

以下是示例源xml文档:

<?xml version="1.0" encoding="UTF-8"?>
<myparag audience="Test_Para" id="para_H56100">
    <title id="title1">First</title>
    <body>
        <p>First paragraph for compilation.
        </p>
    </body>
</myparag>

<?xml version="1.0" encoding="UTF-8"?>
<myparag audience="Test_Para" id="para_H561002">
    <title id="title2">Second</title>
    <body>
        <p><p>Second paragraph for compilation.
        </p>
    </body>
</myparag>

<?xml version="1.0" encoding="UTF-8"?>
<myparag audience="Test_Para id="para_H561009">
    <title id="title3">Third</title>
    <body>
        <p><p>Third paragraph for compilation.
        </p>
        <p>See paragraph one.
        </p>
    </body>
</myparag>

这是所需的合并xml文档

<?xml version="1.0" encoding="UTF-8"?>
<glossgroup audience="Test_Para" id="para_H561080">
<glossaryentry id="dd1">
    <glossterm id="title1">First</glossterm>
    <glossdef>
        <p>First paragraph for compilation.</p>
    </glossdef>
    </glossaryentry>

    <glossaryentry id="dd2">
        <glossterm id="title1">Second</glossterm>
        <glossdef>
            <p>Second paragraph for compilation.</p>
        </glossdef>
    </glossaryentry>

    <glossaryentry id="dd3">
        <glossterm id="title1">Third</glossterm>
        <glossdef>
            <p>Third paragraph for compilation.</p>
            <p>See detail about third paragraph.</p>
        </glossdef>
    </glossaryentry>
    </glossgroup>

1 个答案:

答案 0 :(得分:0)

我修改了你的XSLT以获得所需的输出:

正确的XML输入

<?xml version="1.0" encoding="UTF-8"?>
<myparag audience="Test_Para" id="para_H56100">
  <title id="title1">First</title>
  <body>
    <p>First paragraph for compilation.
    </p>
  </body>
</myparag>

<?xml version="1.0" encoding="UTF-8"?>
<myparag audience="Test_Para" id="para_H561002">
  <title id="title2">Second</title>
  <body>
    <p>Second paragraph for compilation.
    </p>
  </body>
</myparag>

<?xml version="1.0" encoding="UTF-8"?>
<myparag audience="Test_Para" id="para_H561009">
  <title id="title3">Third</title>
  <body>
    <p>Third paragraph for compilation.</p>
    <p>See paragraph one.</p>
  </body>
</myparag>

<强> XSLT:

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

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:param name="files"
    select="collection('file:/C:/Users/vgv/Desktop/Testing/?select=*.dita;recurse=yes')"/>

  <xsl:template match="/">
    <glossgroup audience="Test_Para" id="{concat('para_',generate-id())}">
      <xsl:for-each select="$files//myparag">
        <xsl:sort/>
        <glossaryentry id="{concat('dd', position())}">
          <glossterm id="{concat('title', count(.))}">
            <xsl:value-of select="title"/>
          </glossterm>
          <glossdef>
            <xsl:for-each select="body/p">
              <p><xsl:apply-templates/></p>
            </xsl:for-each>
          </glossdef>
        </glossaryentry>
      </xsl:for-each>
    </glossgroup>
  </xsl:template>
</xsl:stylesheet>

<强>输出:

<glossgroup audience="Test_Para" id="para_d1">
   <glossaryentry id="dd1">
      <glossterm id="title1">First</glossterm>
      <glossdef>
         <p>First paragraph for compilation.
    </p>
      </glossdef>
   </glossaryentry>
   <glossaryentry id="dd2">
      <glossterm id="title1">Second</glossterm>
      <glossdef>
         <p>Second paragraph for compilation.
    </p>
      </glossdef>
   </glossaryentry>
   <glossaryentry id="dd3">
      <glossterm id="title1">Third</glossterm>
      <glossdef>
         <p>Third paragraph for compilation.</p>
         <p>See paragraph one.</p>
      </glossdef>
   </glossaryentry>
</glossgroup>