2010-07-07 43 views
1

我正在使用XSLT操縱XML文件內的數據(我將一個XML文件的內容放入另一個XML文件的新shell中)。我只使用第一個文件中的一些數據,並且將不使用的部分數據連接到新文件的末尾。我如何讓XSLT不這樣做?我的文件末尾的XSLT轉儲垃圾

謝謝!

編輯:下面是一些僞代碼,我不能發佈的實際代碼:

<xsl:output method="xml"/> 
<xsl:template match="foo"> 
    <xsl:element name="bar"> 
     <!--... makes elements and traverses some of the other file ...--> 
    </xsl:element> 
</xsl:template> 

和輸出:

<foo> 
     <bar> 
      <!-- ... --> 
     </bar> 
    </foo> 

    <!-- junk at the end of the file that matches up with the content of the unused data tags --> 

    0 

    N 
    N 
    Y 
    00000148 
    ASDF 
+0

XSLT文件的末尾收集所有其餘的你需要,如果你想好後一些代碼中的空白模板答案。 – 2010-07-07 23:09:14

回答

3

XSLT有一個默認的根匹配模式。要更換它,嘗試這樣的事情在你的XSLT:

<xsl:template match="/"> 
    <xsl:apply-templates select="foo"/> 
</xsl:template>  
<xsl:template match="foo"> 
    <xsl:element name="bar"> 
     <!--... makes elements and traverses some of the other file ...--> 
    </xsl:element> 
</xsl:template> 
+0

+很好的例子,謝謝! – 2010-07-07 23:46:27

+0

我認爲你應該增加你的答案,即垃圾是文本節點的內置模板的結果,就像哈波的答案一樣。 – 2010-07-08 14:14:09

4

XSLT包括某些default templates即我當你不寫一個來覆蓋它的時候,它會被喚醒。例如,如果你不包括模板的根元素匹配,它會做到這一點:

<xsl:template match="/"> 
    <xsl:apply-templates/> 
</xsl:template> 

還有會發出匹配元素的文本內容默認模板。這可能是你在輸出中看到的「垃圾」。

我的猜測是,你需要通過包括根模板,並明確匹配那些你感興趣的元素,以防止這一點。

+0

+很好的解釋! – 2010-07-07 23:46:53

0

僅是完整的 - 你不需要根元素相匹配,如果你不希望這樣。另一種方法,以防止使用內置的默認模板是定義在這樣

<xsl:template match="*" />