2015-10-07 131 views
1

我有問題讓我的模板匹配工作,我試圖遍歷每個使用模板匹配而不是xsl:for-each語句的屬性。xslt模板匹配指導

這裏是xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<?xml-stylesheet type="text/xsl" href="template.xslt"?> 
<x:recording xmlns:x="http://www.example.com/xmlns/record20080320" x:ref="889002005990000" x:version="11.0"> 


    <x:finalized>true</x:finalized> 

    <x:segment> 
    <x:contenttype>mp3</x:contenttype> 
    <x:starttime>2015-07-26T19:15:48.327+04:00</x:starttime> 
    <x:attributes> 
     <x:tag x:timestamp="2015-07-26T19:15:48.719+04:00"> 
     <x:attribute x:key="ref">123456</x:attribute> 
     </x:tag> 
     <x:tag x:timestamp="2015-07-26T19:15:48.719+04:00"> 
     <x:attribute x:key="genre">rock</x:attribute> 
     </x:tag> 
     <x:tag x:timestamp="2015-07-26T19:15:48.719+04:00"> 
     <x:attribute x:key="artist">Anees CK</x:attribute> 
     </x:tag> 
    </x:attributes> 
    <x:systemtype>Windows</x:systemtype> 
    <x:multipart> 
     <x:primary>889002005990000</x:primary> 
    </x:multipart> 
    <x:duration>6</x:duration> 
    </x:segment> 

</x:recording> 

這是我的模板,

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.example.com/xmlns/record20080320" > 
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" /> 
<xsl:template match="x:recording"> 
    <Call xmlns:xsi="http://www.w3.org/20001/XMLSchema-instance"> 
     <Data> 
      <finalized> 
       <xsl:value-of select="x:finalized"/> 
      </finalized> 
      <test111> 
       <xsl:text>some text</xsl:text> 
      </test111> 
      <contenttype> 
       <xsl:value-of select="x:segment/x:contenttype"/> 
      </contenttype> 
      <sometag> 
       <xsl:value-of select="x:segment/x:attributes/x:tag/x:attribute"/> 
      </sometag> 

      <xsl:template match="x:attributes"> 
       <xsl:text>found attribute</xsl:text>   
      </xsl:template> 

     </Data> 
    </Call> 
</xsl:template> 
</xsl:stylesheet> 

有人能看到我在做什麼錯? 在此先感謝。

+0

模板不能嵌套。你在找什麼結果? –

回答

1

也許你打算在:

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.example.com/xmlns/record20080320" > 
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" /> 
<xsl:template match="x:recording"> 
    <Call xmlns:xsi="http://www.w3.org/20001/XMLSchema-instance"> 
     <Data> 
      <finalized> 
       <xsl:value-of select="x:finalized"/> 
      </finalized> 
      <test111> 
       <xsl:text>some text</xsl:text> 
      </test111> 
      <contenttype> 
       <xsl:value-of select="x:segment/x:contenttype"/> 
      </contenttype> 
      <sometag> 
       <xsl:value-of select="x:segment/x:attributes/x:tag/x:attribute"/> 
      </sometag> 
      <xsl:apply-template select="x:attributes"/> 

     </Data> 
    </Call> 
</xsl:template> 

<xsl:template match="x:attributes"> 
    <xsl:text>found attribute</xsl:text>   
</xsl:template> 

</xsl:stylesheet> 

(我花了一些時間來鍛鍊身體,當你使用「屬性」,你不是說XML屬性,你的意思是元素,其名稱是「X:屬性」)。

+1

'還是我?' – kjhughes

+0

謝謝邁克爾,但我已經嘗試過,它不起作用。理想情況下,我想遍歷x:attributes下的元素,然後過濾某些元素,例如只選擇流派。 – Chris

+0

你究竟嘗試了什麼,它是如何失敗的?我並沒有試圖告訴你一個完整的解決方案:我只是告訴你在哪裏放置'xsl:template'聲明和'xsl; apply-templates'聲明。 –