2014-02-28 42 views
-1

我需要使用XSL在xml中的兩個元素之間提取幾個元素,並將提取的內容寫入到xml文件中。如何使用XSL 2.0選擇xml文件中兩個特定元素之間的多個元素

我有下面的XML。

<?xml version="1.0" encoding="UTF-8"?> 
    <root> 
    <element_1> 
     <h1>title</h1> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <table> 
     <tr> 
      <td /> 
     </tr> 
     </table> 
     <h1>Another Title</h1> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <table> 
     <tr> 
      <td/> 
     </tr> 
     </table> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <table> 
     <tr> 
      <td/> 
     </tr> 
     </table> 
     <h1>Some other Title</h1> 
     <p>paragraph</p> 
     <table> 
     <tr> 
      <td/> 
     </tr> 
     </table> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <table> 
     <tr> 
      <td/> 
     </tr> 
     </table> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
    </element_1> 
    <element_2/> 
</root> 

XSL是新的給我,並面臨生成與提取的內容的新文件的難度。不知何故,我可以管理生成新文件,但無法提取兩個特定()標籤之間的標籤。以上XML是第三方工具的輸出。

請分享您的想法或者如果有人有想法提取標籤之間的元素?

預期的輸出應該看起來像下面:

File1.xml:

<modified> 
    <h1>title</h1> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <table><tr><td/></tr></table> 
</modified> 

File2.xml:

<modified> 
     <h1>Another Title</h1> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <table><tr><td/></tr></table> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <p>paragraph</p> 
     <table><tr><td/></tr></table> 
    </modified> 

File3.xml:

<modified> 
    <p>paragraph</p> 
    <table><tr><td/></tr></table> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <table><tr><td/></tr></table> 
    <p>paragraph</p> 
    <p>paragraph</p> 
    <p>paragraph</p> 
</modified> 

謝謝。

回答

1

您可以使用下面的樣式表:

<?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"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="element_1"> 
     <xsl:for-each-group select="*" group-starting-with="h1"> 
      <xsl:result-document href="{concat('File', count(preceding-sibling::h1) + 1, '.xml')}"> 
       <modified> 
        <xsl:apply-templates select="current-group()"/> 
       </modified> 
      </xsl:result-document> 
     </xsl:for-each-group> 
    </xsl:template>  

</xsl:stylesheet> 
+0

這組節點以'h1'並將其輸出到一個文件名'文件[否] .xml' –

+0

應該足夠使用'的'爲第一組產生'File1.xml',爲第二組產生'File2.xml',等等。 –

+0

感謝您的回覆。 @Joel M. Lamsen,但當我在我的系統中嘗試XSL時,我得到了這個錯誤:致命錯誤:java.lang.IllegalArgumentException:URI方案不是「文件」 ; SystemID:file:/// C:/projects/XSL/XSL_Transformation.xsl;行號:8; Column#:-1 轉換過程中出錯 net.sf.saxon.trans.XPathException:java.lang.IllegalArgumentException:URI方案不是「文件」 – Kepler

相關問題