Amit wrote:
I have a quick question. Given test XML:
<root>
<ns1:sub>1</ns1:sub>
<ns2:sub>2</ns2:sub>
<ns3:sub>3</ns3:sub>
</root>
is there a way to select all nodes that contain the word "sub". (Due
to namespace constraints, I will be getting the subnodes as <ns1:sub>,
<ns2:sub> and so on.)
regards,
Amit.
You can use the XPath function 'contains' to search for 'sub' within the
element names.
For this XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sub.xsl"?>
<root>
<ns1:sub>1</ns1:sub>
<ns2:sub>2</ns2:sub>
<ns3:sub>3</ns3:sub>
<ns1:nomatch>1</ns1:nomatch>
<ns2:nomatch>2</ns2:nomatch>
<ns3:nomatch>3</ns3:nomatch>
</root>
The following XSL transformation selects those 'sub' elements:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/root">
<html><head/>
<body>
<xsl:for-each select="*">
<xsl:if test="contains(name(.),'sub')">
<h2><xsl:value-of select="name(.)"/></h2>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Hopefully that is what you were looking for. Have fun!
-Scott