xslt模板匹配指导

时间:2015-10-07 20:17:40

标签: xml xslt

我在模板匹配工作时遇到问题,我试图使用模板匹配而不是xsl:for-each语句迭代每个属性。

这是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>

谁能看到我做错了什么? 提前谢谢。

1 个答案:

答案 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:attribute”的元素。)

相关问题