XSLT - match a variable element in a predicate
<xsl:apply-templates select="element[child='Yes']">
Works great, but I would like to use
<xsl:apply-templates select="element[$childElementName='Yes']">
so I can use a variable to indicate node.
for instance
<xsl:apply-templates select="theList/entity[Central='Yes']">
works great:
<?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>
But I do not want to hardcode the name of the child.
Any suggestions?
Thanks Tim for the answer:
<xsl:apply-templates select="theList/entity[child::*[name()=$childElement]='Yes']" />
You can check the element name using the local-name () function, for example
<xsl:apply-templates select="theList/entity[child::*[name()='Central']='Yes']" />
This checks all child nodes that have the name "Central"
Then you can easily replace hard coding with a parameter or variable. Thus, if you use the following XSLT to enter XML:
<?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>
You will get a conclusion
<Name>Company 1</Name><Name>Company 3</Name>