2016-01-15 44 views
-2

我有一個輸入文檔,我只想提取帶有前綴ppp的元素。前綴爲ppp的所有元素都處於同一級別。在XSLT中創建根元素

輸入:

<root> 
    <ppp:element>aaa</ppp:element> 
    <ppp:element>ccc</ppp:element> 
    <lala:element>PPP</lala:element> 
    <rrr:element>dsfsdbfsdf</rrr:element> 
</root> 

在我的XSLT我所有的元素複製帶有前綴ppp到輸出文件。

問題是我在輸出文件中沒有root元素。

所以我需要創建一個root元素。在root元素中,我應該複製所有具有前綴ppp的元素。

我的XSLT:

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

所需的輸出:

<root> 
    <ppp:element>aaa</ppp:element> 
    <ppp:element>ccc</ppp:element> 
</root> 
+0

這不能成爲你輸入:你不能有一個前綴不綁定到一個命名空間。同樣適用於輸出。 –

回答

0

給出一個形成良好輸入,所有的樣式表必須做的是:

<xsl:template match="/root"> 
    <xsl:copy> 
     <xsl:copy-of select="ppp:*"/> 
    </xsl:copy> 
</xsl:template> 
0

更通用的選項是放棄所有元素,無論它們在XML樹中的什麼位置,如果它們的前綴是ppp(無論名稱空間URI如何 - 這是不好的做法!但它是嚴格你問的),同時保留輸入根:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ppp="http://example.org"> 

    <xsl:output method="xml" /> 

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

    <xsl:template match="*[substring-before(name(), ':') != 'ppp' and not(. = /)]" /> 

</xsl:stylesheet> 

這裏是XSLT轉換演示:http://xsltransform.net/6r5Gh3y/2

-1

Thnx。

<xsl:template match="/"> 
    <ppp:name> 
     <xsl:apply-templates select="node()"></xsl:apply-templates> 
    </ppp:name> 

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

上述工作對我來說:)

+0

這不提供請求的輸出。它不會複製'ppp:element'節點中包含的文本節點 - 但它會複製整個XML輸入中的所有其他文本節點。 –