XPath: finding nodes that have an attribute containing a word
-
I'm trying to write an XPath query to match a node that has an attribute that contains a certain word. The obvious *[@attr="findme"] only works if the target attribute consists of *only* the word "findme". My target XML nodes might have the attribute "foo findme bar". Using *[contains(@attr,"findme")] doesn't quite work either since it will also match a node if the attribute contains "x-findme", for example. The best I've been able to come up with is this:
\*\[contains(concat(" ",concat(@class," ")," findme ")\]
but this is obviously clumsy. Is there a better way e.g. some way I can define my own XPath extension function called contains_word() that I can get MXSML to invoke?
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
-
I'm trying to write an XPath query to match a node that has an attribute that contains a certain word. The obvious *[@attr="findme"] only works if the target attribute consists of *only* the word "findme". My target XML nodes might have the attribute "foo findme bar". Using *[contains(@attr,"findme")] doesn't quite work either since it will also match a node if the attribute contains "x-findme", for example. The best I've been able to come up with is this:
\*\[contains(concat(" ",concat(@class," ")," findme ")\]
but this is obviously clumsy. Is there a better way e.g. some way I can define my own XPath extension function called contains_word() that I can get MXSML to invoke?
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
Actually, there's an XPath function called "matches" that takes a regular expression: http://www.w3.org/TR/2005/CR-xpath-functions-20051103/#func-matches[^]
-
Actually, there's an XPath function called "matches" that takes a regular expression: http://www.w3.org/TR/2005/CR-xpath-functions-20051103/#func-matches[^]
-
That is XPath 2.0 and I am not aware of any support for it yet, is there?
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?"
Colin Angus Mackay in the C# forumled mike
Ya, you're right. Unfortunately Microsoft sucks and doesn't support it. Of course apache and altova can read XPath 2. There aren't any cool extension functions that can stand in place of that either. I guess another thing that can be done is to sneak some script into the style sheet (if you're using a style sheet).
<msxsl:script language="JavaScript" implements-prefix="my-prefix">
<![CDATA[
function SomeFunction(text) { ... }
]]>
</msxsl:script>
...
<xsl:if test="my-prefix:SomeFunction('some text')">...
-
Ya, you're right. Unfortunately Microsoft sucks and doesn't support it. Of course apache and altova can read XPath 2. There aren't any cool extension functions that can stand in place of that either. I guess another thing that can be done is to sneak some script into the style sheet (if you're using a style sheet).
<msxsl:script language="JavaScript" implements-prefix="my-prefix">
<![CDATA[
function SomeFunction(text) { ... }
]]>
</msxsl:script>
...
<xsl:if test="my-prefix:SomeFunction('some text')">...
And I just fired up my IDE to give it a go. Sigh... :-( Thanks for the suggestion anyway.
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2.2 [^]: A free RSS/Atom feed reader with support for Code Project.