【问题标题】:xslt template match guidancexslt 模板匹配指导
【发布时间】:2015-10-07 20:17:40
【问题描述】:

我在使模板匹配正常工作时遇到问题,我正在尝试使用模板匹配而不是 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>

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

【问题讨论】:

  • 模板不能嵌套。您要寻找的结果是什么?

标签: xml xslt


【解决方案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”的元素)。

【讨论】:

  • &lt;attribute element="Am I an attribute value?"&gt;Or am I?&lt;/attribute&gt;
  • 谢谢迈克尔,但我已经尝试过了,但它不起作用。理想情况下,我想遍历 x:attributes 下的元素,然后过滤某些元素,例如只选择流派。
  • 您到底尝试了什么,又是如何失败的?我并不是要向您展示一个完整的解决方案:我只是向您展示在哪里放置 xsl:template 声明和 xsl;apply-templates 声明。
猜你喜欢
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多