XSL套用范本输出问题 [英] XSL apply-templates output issues
问题描述
给出此XML:
<?xml version="1.0" encoding="iso-8859-2" ?>
<products>
<p>
<id> 50 </id>
<name> Murphy </name>
<price> 33 </price>
</p>
<p>
<id> 40 </id>
<name> Eddie </name>
<price> 9999 </price>
</p>
<p>
<id> 20 </id>
<name> Honey </name>
<price> 9999 </price>
</p>
<p>
<id> 30 </id>
<name> Koney </name>
<price> 11 </price>
</p>
<p>
<id> 10 </id>
<name> Margarethe </name>
<price> 11 </price>
</p>
</products>
使用此XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="p[id > 20]">
<idKP> <xsl:value-of select="id"/></idKP>
<skitter><xsl:value-of select="name"/></skitter>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
具有以下输出:
<?xml version="1.0"?>
<idKP>50</idKP><skitter>Murphy</skitter>
<idKP>40</idKP><skitter>Eddie</skitter>
20
Honey
9999
<idKP>30</idKP><skitter>Koney</skitter>
10
Margarethe
11
问:为什么有些人的价值观不匹配?
20, Honey, 9999, ...
Q : Why there are values of those who did not matched?
20, Honey, 9999, ...
推荐答案
由于内置模板规则-当没有与特定节点匹配的显式模板时,将使用内置规则,对于元素节点表示<xsl:apply-templates/>
,对于文本节点表示<xsl:value-of select="."/>
.这两个规则结合在一起的作用是输出元素下面的所有文本,而不是元素标签本身.
Because of the built-in template rules - when there are no explicit templates to match a particular node, the built-in rules are used, which for element nodes means <xsl:apply-templates/>
and for text nodes means <xsl:value-of select="."/>
. The effect of these two rules in combination is to output all the text under the elements but not the element tags themselves.
您可以添加第二个禁止操作模板
You could add a second do-nothing template
<xsl:template match="p" />
完全忽略与您的条件不匹配的p
元素.显式模板,甚至什么都不做,比默认的内置规则更受欢迎.
to completely ignore p
elements that don't match your condition. An explicit template, even a do-nothing one, is preferred over the default built-in rule.
这篇关于XSL套用范本输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!