0

我有以下 xml 文件,我正在尝试使用身份转换进行复制。我想要做的是使用集合功能将所有源文件的所有内容合并到一个xml文件中。我还需要在所有 and 元素上使用 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>
4

1 回答 1

0

我已经修改了您的 XSLT 以获得所需的输出:

正确的 XML(s) 输入

<?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>
于 2013-06-06T05:53:01.990 回答