xsl:template match属性:与默认名称空间的关系 [英] xsl:template match attribute: how related to default namespace

查看:152
本文介绍了xsl:template match属性:与默认名称空间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当根元素具有默认名称空间属性而不是默认名称空间属性时,我在xslt行为上遇到了特殊的差异.
我想知道为什么会出现这种差异.

I encountered a peculiar difference in xslt behavior when the root element has a default namespace attribute as opposed to when it does not.
I am wondering why this difference occurs.

XML输入是

<root>
    <content>xxx</content>
</root>

应用以下转换时

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="content">
        <w>x</w>
    </xsl:template>

</xsl:stylesheet>

结果是预期的

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <w>x</w>
</root>

但是当将相同的变换应用于

But when the same transformation is applied to

<root xmlns="http://test.com">
    <content>xxx</content>
</root>

结果有所不同,并且仅基于默认模板的应用(这些模板有效地输出了文本节点值"xxx"):

the result is different and based on the application of just default templates (which effectively output the text node value 'xxx'):

<?xml version="1.0" encoding="UTF-8"?>
<root>xxx</root>

添加

如果在这种情况下这是预期的行为,那么在第二种情况下需要什么match属性值才能匹配content元素?

When this is the expected behavior in this case, then what match attribute value is needed to match the content element in the second case?

推荐答案

那么您的问题到底是什么?如果您只是在寻找一种解释,下面是一个简短的解释.您正在观察的是根据规范的正确行为.当您将名称空间放在某物上时,解析器实质上将其完全视为一个不同的元素(而不是具有相同名称但没有名称空间的元素).因此,在第二种情况下,当您说<xsl:template match="content">时,它与XML文件中的<content>元素不匹配,因为它属于http://test.com命名空间(通过其父级的命名空间声明).因此,默认模板将接管.

so what exactly is your question? If you're simply looking for an explanation, following is a brief one. What you're observing is the proper behavior according to the specification. When you put a namespace on something, the parser essentially treats it as a different element entirely (than an element of the same name but no namespace). Therefore, in the second situation, when you say <xsl:template match="content">, it doesn't match the <content> element in the XML file because it falls under the http://test.com namespace (by way of the namespace declaration on its parent). Therefore, the default templates take over.

这篇关于xsl:template match属性:与默认名称空间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆