2011-03-12 111 views
2

目前,我有下面的XML文件:XSL轉換的幫助

<Regions> 
    <Region> 
    <Code>AU</Code> 
    <Name>Austria</Name> 
    </Region> 
</Regions> 
<Channels> 
    <Channel> 
     <Code>00</Code> 
     <Name>Im a channel</Name> 
    </Channel> 
    ... 
</Channels> 
<Programs> 
    <Program> 
     <Code>00</Code> 
     <Name>Program</Name>   
    </Program> 
</Programs> 

我只想保持通道路徑,使輸出看起來像這樣使用XSLT:

<Channels> 
    <Channel> 
     <Code>00</Code> 
     <Name>Im a channel</Name> 
    </Channel> 
    ... 
</Channels> 
+1

你能給我們一個你想要輸出看起來像樣的例子嗎? – 2011-03-12 23:04:43

+0

你在說什麼「路徑」? – ChrisJ 2011-03-12 23:07:45

+0

詳細闡述了一下,對不起沒有太多的信息。 – schone 2011-03-12 23:30:51

回答

2

此轉換:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

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

<xsl:template match="/t/*[not(self::Channels)]"/> 
</xsl:stylesheet> 

當所提供的XML文檔施加(固定爲進行良好的形成):

<t> 
    <Regions> 
     <Region> 
      <Code>AU</Code> 
      <Name>Austria</Name> 
     </Region> 
    </Regions> 
    <Channels> 
     <Channel> 
      <Code>00</Code> 
      <Name>Im a channel</Name> 
     </Channel>  
    </Channels> 
    <Programs> 
     <Program> 
      <Code>00</Code> 
      <Name>Program</Name> 
     </Program> 
    </Programs> 
</t> 

產生想要的,正確的結果:

<Channels> 
    <Channel> 
     <Code>00</Code> 
     <Name>Im a channel</Name> 
    </Channel> 
</Channels> 

說明

使用身份規則,覆蓋for頂部元素(傳遞)以及頂部元素的任何非子項(刪除)。