如何在xsl:template中分配名称空间

时间:2016-01-07 11:45:47

标签: xml xslt namespaces

我为xsl:模板和名称空间的第100个问题道歉。我确实阅读了其他问题(例如hereherehere),但仍然无法将这些问题放在一起。

这是我的意见:

<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT version="6.2.0" xmlns="http://www.authorit.com/xml/authorit"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
    <Objects>
        <Book>
            <Object>
                <Description>1089-802-01</Description>
            </Object>
        </Book>
    </Objects>
</AuthorIT>

这是我的样式表:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ait="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
<xsl:template match="/">
    <Table>
        <apply-templates/>
    </Table>
</xsl:template>
<xsl:template match="ait:Book">
    <Row>
        <Cell>
            <Data><xsl:value-of select="./Object/Description"/></Data>
        </Cell>
    </Row>
</xsl:template>

预期结果:

<Table>
    <Row>
        <Cell>
            <Data>1089-802-01</Data>
        </Cell>
    </Row>
</Table>

实际结果:

<Table>
       1089-802-01
</Table>

换句话说,xsl:template match =&#34; /&#34;匹配,但xsl:模板匹配=&#34; ait:Book&#34;没有。

如何匹配Book元素?

请注意,我从输入根元素AuthorIT xmlns =&#34; http://www.authorit.com/xml/authorit" 中取出了默认命名空间,并将其添加到样式表为 xmlns:ait =&#34; http://www.authorit.com/xml/authorit" 。 然后我尝试将Book元素与 ait:Book 相匹配。但我必须误解this answer

我不知道它是否重要,但我使用的是Saxon HE 9和Notepad ++ XML插件。

4 个答案:

答案 0 :(得分:0)

<apply-templates/>是一个文字结果元素,您需要<xsl:apply-templates/>。但我没有得到你说的结果(http://xsltransform.net/ej9EGcB),我得到了

<?xml version="1.0" encoding="UTF-8"?><Table xmlns:ait="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><apply-templates/></Table>

清楚地表明您只需创建一个带有Table子元素的apply-templates元素。

答案 1 :(得分:0)

正如Martin Honnen已经指出的那样,使用您的代码收到的实际结果并不是您所声称的。

无论如何,要获得预期结果,您必须更改

<receiver
    android:name=".Salva_autostart"
    android:enabled="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >

    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.xxx.my_filter_intent" />
    </intent-filter>
</receiver>

为:

<xsl:value-of select="./Object/Description"/> 

因为这些元素也在给定的命名空间中。

您还应该添加<xsl:value-of select="./ait:Object/ait:Description"/> 作为exclude-result-prefixes="ait"元素的属性,并删除那里的冗余命名空间声明。

这是xsl:stylesheet<apply-templates/>问题的补充。

当然,您需要关闭<xsl:apply-templates/>代码。

答案 2 :(得分:0)

谢谢大家的答案。

我做了以下事情:

  • xmlns:ait="http://www.authorit.com/xml/authorit"声明添加到样式表标记。
  • 在相关的位置添加了ait:前缀。
  • 在样式表标记中添加了exclude-result-prefixes="ait"

它现在有效,虽然我不明白exclude-result-prefixes="ait"的作用:转换在有和没有它的情况下都很顺利。也许是未来研究的主题。

<apply-templates/><xsl:apply-templates/>问题以及结束</xsl:stylesheet>标记是在简化帖子文件时插入的拼写错误。对此感到抱歉,感谢你指出。

特别感谢Martin Honnen与http://xsltransform.net的链接:我不知道它存在并且非常方便。

答案 3 :(得分:-1)

尝试使用

<xsl:apply-templates/>

(你错过了&#34; xsl:&#34;) 和

<xsl:template match="AuthorIT/Object/Books">

而不是&#34; ait:Book&#34;。 这应该可以解决问题。

相关问题