Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using wildcards in <xsl:match="myTag*">

Tags:

xml

xslt

I'm trying to create an xsl template that will accept wildcards as part of the template match as the following example will show:

<xsl:template match="*_Nokia_5.0">
  <xsl:value-of select="."/>
  <xsl:text>,</xsl:text>
  <xsl:if test="position()=last()">
    <xsl:text>&#10;</xsl:text>
  </xsl:if>

What I'm trying to achieve is 'match any tag that has 'Nokia_5.0' as part of the string. Is there any way I can do this in xslt 1.0?

Thanks

like image 360
yorkshireflatcap Avatar asked Dec 30 '25 00:12

yorkshireflatcap


2 Answers

Using contains(name(), "Nokia_5.0") is not the solution, as it will return false positives on names like:

myNokia_5.0isAwsome

however the requirement is that the name must end with the string 'Nokia_5.0'.


A correct solution:

In XSLT 2.0:

<xsl:template match="*[ends-with(name(), 'Nokia_5.0')]">

In XSLT 1.0:

<xsl:template match=
 "*[substring(name(), string-length(name()) -8) = 'Nokia_5.0')]">
like image 50
Dimitre Novatchev Avatar answered Dec 31 '25 12:12

Dimitre Novatchev


Assuming you mean the node name must contain the given string, you can use contains()

<xsl:template match='*[contains(name(), "Nokia_5.0")]'>

However this would mean that any nodes that DON'T conform to this requirement lack a template, and would be output as-is if you applied templates to all nodes.

Instead, you could make the filter stipulation not in the template definition but at the point of applying templates.

<xsl:apply-templates select='node[contains(name(), "Nokia_5.0")]' />
....
<xsl:template match='node'>
    <xsl:value-of select='.' />
</xsl:template>
like image 35
Mitya Avatar answered Dec 31 '25 12:12

Mitya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!