Xml XSLT按名称选择当前节点的所有三级子节点
我试图使用应用模板从多个相似的父节点迭代特定的子节点。示例如下:Xml XSLT按名称选择当前节点的所有三级子节点,xml,xslt,child-nodes,Xml,Xslt,Child Nodes,我试图使用应用模板从多个相似的父节点迭代特定的子节点。示例如下: <test_report xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <countries> <country name='sample1'> <education> <men>
<test_report
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<countries>
<country name='sample1'>
<education>
<men>
<literacyRates>25,36,43</literacyRates>
</men>
</education>
</country>
<country name='sample2'>
<education>
<men>
<literacyRates>45,46,55,56</literacyRates>
</men>
</education>
</country>
</countries>
现在,我想对属于countries元素的所有literacyRates元素应用一个模板。我尝试了以下XSL:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="countries">
<xsl:call-template name="countries" />
</xsl:template>
<xsl:template name="countries" match="literacyRate">
<xsl:call-template name="testoutput"><xsl:with-param name="list" select="." /> </xsl:call-template>
]
</xsl:template>
<xsl:template name="testoutput">
<xsl:param name="list" select="."/>
list=<xsl:value-of select="$list" />
</xsl:template>
我本来希望以list=开头的输出行出现两次,因为我们有两个匹配的literacyRate元素,但事实并非如此。此处需要一些帮助。请尝试以下脚本:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="country//literacyRates">
<xsl:text>list=</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
请尝试以下脚本:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="country//literacyRates">
<xsl:text>list=</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
现有XSL代码
您的XSL有许多问题
您使用调用模板的方式没有多大意义。使用apply模板将更加惯用
您有三个模板,但实际上您只在最后一个模板中执行任何操作,并且该模板只输出元素的字符串值。最好只有一个模板:
<xsl:template match="countries">
list=<xsl:value-of select="."/>
</xsl:template>
仅当您在处理流的前面使用应用模板时,“匹配”属性才起作用。因为您只使用调用模板,所以match属性不起任何作用。
此外,由于match属性指定了literacyRate,但您的输入XML只包含literacyRates元素,因此即使您之前使用了apply模板,这仍然不会起任何作用
一种工作方法:文本输出
看起来您希望每个元素都有一个list=行。要将此输出生成为纯文本,请执行以下操作:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="countries">
<xsl:apply-templates select="country//literacyRates"/>
</xsl:template>
<xsl:template match="literacyRates">
<xsl:text>list=</xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
现有XSL代码
您的XSL有许多问题
您使用调用模板的方式没有多大意义。使用apply模板将更加惯用
您有三个模板,但实际上您只在最后一个模板中执行任何操作,并且该模板只输出元素的字符串值。最好只有一个模板:
<xsl:template match="countries">
list=<xsl:value-of select="."/>
</xsl:template>
仅当您在处理流的前面使用应用模板时,“匹配”属性才起作用。因为您只使用调用模板,所以match属性不起任何作用。
此外,由于match属性指定了literacyRate,但您的输入XML只包含literacyRates元素,因此即使您之前使用了apply模板,这仍然不会起任何作用
一种工作方法:文本输出
看起来您希望每个元素都有一个list=行。要将此输出生成为纯文本,请执行以下操作:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="countries">
<xsl:apply-templates select="country//literacyRates"/>
</xsl:template>
<xsl:template match="literacyRates">
<xsl:text>list=</xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
如果您的目标是应用模板,那么您需要所有这些xsl:call模板做什么?match=countries可以匹配countries元素,然后将模板应用于literacyRates子体,只需在匹配的模板中使用这些元素,然后输出您需要输出的内容。如果您的目标是应用模板,那么所有这些xsl:call模板的用途是什么?match=countries可以匹配countries元素,然后,要将模板应用于literacyRates子体,只需在这些元素的匹配模板中使用,然后输出需要输出的内容。