2013-06-05 126 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