Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. XPath: finding nodes that have an attribute containing a word

XPath: finding nodes that have an attribute containing a word

Scheduled Pinned Locked Moved XML / XSL
xmldatabasecomregextutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Taka Muraoka
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • T Taka Muraoka

      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.

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      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[^]


      Logifusion[^]

      L 1 Reply Last reply
      0
      • D Dustin Metzgar

        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[^]


        Logifusion[^]

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        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# forum

        led mike

        D 1 Reply Last reply
        0
        • L led mike

          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# forum

          led mike

          D Offline
          D Offline
          Dustin Metzgar
          wrote on last edited by
          #4

          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')">...


          Logifusion[^]

          T 1 Reply Last reply
          0
          • D Dustin Metzgar

            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')">...


            Logifusion[^]

            T Offline
            T Offline
            Taka Muraoka
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups