1

In this post, the user is looking to match nodes that have a certain substring in one of their attributes

I would like to match attributes that contains a certain substring (not the node), but have been unable to come up with the syntax that would do that.

My end goal is to find specific substrings in an attribute, and then replace those substrings with different strings.

I've tried using @[contains(.,"substring")], but that didn't work.

I am using XSLT 3.0

Community
  • 1
  • 1
Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
  • You ask about "matching" nodes in XSL/T, suggesting *match patterns*, but you link to a post that's exclusively about *selecting* nodes in XPath. These are two slightly different issues. I don't think it makes any difference for this question, but at some point you may get wrong answers if you ask about one when you mean the other. – LarsH May 27 '15 at 21:00

2 Answers2

3

You need to use @*:

@*[contains(.,"substring")]

@ is the abbreviation for the attribute:: axis. You can't have an axis specifier without a nodetest. * supplies the nodetest.

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • This matches the node that has the attribute. I'm looking to match the attribute itself (not the node) – Nathan Merrill May 27 '15 at 21:18
  • @Nathan, note that attributes *are* nodes. I assume when you say "not the node," you mean "not the element." This XPath expression selects attribute nodes, not element nodes (that have an attribute). Maybe if you give an example of the XML you're trying to select an attribute out of, I can help you find the XPath you need. – LarsH May 27 '15 at 21:24
  • Ok. I assumed that * matches element nodes, and didn't realize that attributes are considered nodes. Thank you for your help! – Nathan Merrill May 27 '15 at 21:32
1

You are almost there. For this input XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <node attribute="ABC">XYZ</node>
</root>

The XSLT code looks like this:

  <xsl:for-each select="//node">
    <xsl:if test="contains(./@attribute, 'ABC')">
      Yes - <xsl:value-of select="./@attribute"/>
    </xsl:if>
  </xsl:for-each>

And the output is:

Yes - ABC
keenthinker
  • 7,645
  • 2
  • 35
  • 45