XSLT 默认模板混淆 [英] XSLT default template confusion
问题描述
我对 XSLT 处理器节点的方式感到困惑,假设我有一个这样的 XML 文档:
I'm getting a confusion on the way XSLT processors nodes, suppose I have an XML Doc like this:
<object>
<animal>
<man men="asd">man1</man>
<man>man2</man>
<man>man3</man>
<man>man4</man>
<cat>cat1</cat>
<cat>cat2</cat>
<cat>cat3</cat>
<cat>cat4</cat>
</animal>
<vehicule>
<car>car1</car>
<car>car2</car>
<car>car3</car>
<car>car4</car>
</vehicule>
</object>
当我有一个没有任何模板匹配的 XSLT 时,如下所示,它返回所有文本节点而没有属性节点,没关系
When I have an XSLT without any template matching like the one below, it returns all text nodes and no attribute nodes, that's OK
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
</xsl:stylesheet>
但是当我有一个像下面这样的时,它不会返回任何东西:
But when I have one like the one below, it doesn't return anything:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="object">
</xsl:template>
</xsl:stylesheet>
是不是如果我有一个父节点的显式模板,我就应该有一个父节点的所有子节点的显式模板?
Is it that if I have an explicit template for a parent node, I should have an explicit template for all child nodes of the parent nodes?
推荐答案
您所看到的只是 内置规则,输出节点的文本值并将模板应用于其所有子节点.
What you are seeing are simply the effects of the built-in rules, which output the text value of a node and apply the templates to all its children.
如果您覆盖内置模板,那么您的模板就会生效.您想应用所有子项的内置规则<代码>对象代码>:
If you overwrite the built-in templates, well, your template takes effect. You want to apply the built-in rules for all children of object
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="object">
<xsl:apply-templates select="*" />
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT 默认模板混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!