2013-06-05 42 views
0

我尝试使用标识转换来复制以下xml文件。 我想要做的是使用集合函数将所有源文件的所有内容合并到一个xml文件中。我还需要在所有元素上使用generate-id()。 我已阅读此论坛和其他地方,但由于我仍在使用我的XSLT,因此我很难获取所需的文档。使用一个XSLT转换多重XML文件

我已经看过几个例子,但是他们没有一个能够做我所需要的。

下面是我使用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> 

回答

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> 
+0

谢谢你纠正我提交XSLT很多纳文。它正在产生所需的结果。 – ManUO