<xsl:apply-templates select="element[child='Yes']">运行正常,但我想使用
<xsl:apply-templates select="element[$childElementName='Yes']">所以我可以使用一个变量来指定节点。
例如
<xsl:apply-templates select="theList/entity[Central='Yes']">在以下情况下工作正常:
<?xml version="1.0" encoding="utf-8"?>
<theList>
<entity>
<Business-Name>Company 1</Business-Name>
<Phone-Number>123456</Phone-Number>
<Central>Yes</Central>
<region1>No</region1>
<region2>Yes</region2>
<region3>No</region3>
<Northern>No</Northern>
</entity>
<entity>
<Business-Name>Company 2</Business-Name>
<Phone-Number>123456</Phone-Number>
<Central>No</Central>
<region1>Yes</region1>
<region2>No</region2>
<region3>No</region3>
<Northern>Yes</Northern>
</entity>
<entity>
<Business-Name>Company 3</Business-Name>
<Phone-Number>123456</Phone-Number>
<Central>Yes</Central>
<region1>No</region1>
<region2>No</region2>
<region3>No</region3>
<Northern>No</Northern>
</entity>
<entity>
<Business-Name>Company 4</Business-Name>
<Phone-Number>123456</Phone-Number>
<Central>No</Central>
<region1>No</region1>
<region2>No</region2>
<region3>No</region3>
<Northern>No</Northern>
</entity>
</theList>但我不希望硬编码子元素名称。
有什么建议吗?
感谢Tim的回答:
<xsl:apply-templates select="theList/entity[child::*[name()=$childElement]='Yes']" />发布于 2010-07-27 15:22:48
可以使用local- name ()函数测试元素的名称,如下所示
<xsl:apply-templates select="theList/entity[child::*[name()='Central']='Yes']" />这将检查名称为'Central‘的所有子节点
然后,您可以轻松地将硬编码替换为参数或变量。因此,如果在XML输入上使用以下XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="childElement">Central</xsl:param>
<xsl:template match="/">
<xsl:apply-templates select="theList/entity[child::*[name()=$childElement]='Yes']" />
</xsl:template>
<xsl:template match="entity">
<Name><xsl:value-of select="Business-Name" /></Name>
</xsl:template>
</xsl:stylesheet>您将获得输出
<Name>Company 1</Name><Name>Company 3</Name>发布于 2010-07-27 21:08:12
使用的
theList/entity/*[name() = $childElementName][. = 'Yes']https://stackoverflow.com/questions/3341122
复制相似问题