XPath: selecting on namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kj

    #1

    XPath: selecting on namespaces



    Suppose I have some XML document that contains tags of the form

    <... xmlns:foo="http ://www.bar.org/foo">

    <... xmlns:foo="baz" >

    <... xmlns:frobozz=" http://www.bar.org/foo">

    What's the XPath expression to select the namespace nodes with
    prefix "foo"? And what's the XPath expression to select the
    namespace nodes with value "http://www.bar.org/foo"?

    I am using the Perl module XML::XPath. With it, I know how to
    select namespace nodes with value "baz" (using
    $xpath_obj->find('//namespace::baz' )), but I get an error if I try
    to select namespace nodes with value "http://www.bar.org/foo" using
    $xpath_obj->find('//namespace::http ://www.bar.org/foo'). I can
    see why the latter would be problematic, since at least the slashes
    in the namespace's value would collide with XPath syntax, but I
    don't know how to escape them. (Actually, the parser seems to
    choke on the colon after http before it gets to do so on the
    slashes.)

    With a full implementation of the XML::XPath standard, I suppose
    I could try something like
    $xpath_obj->find('namespac e-uri(//namespace::*)=" http://www.bar.org/foo"')),
    but when I do this I get the error "namespace-uri: Function not
    supported".

    I have no clue how to use XML::XPath to select namespace nodes with
    prefix "foo".

    Thanks!

    kj

    P.S. Is there a "standard" forum for XPath-related queries like
    this one?


    --
    NOTE: In my address everything before the period is backwards.
  • Dimitre Novatchev  [MVP XML]

    #2
    Re: XPath: selecting on namespaces


    "kj" <socyl@987jk.co m> wrote in message
    news:c5dkrg$mak $1@reader1.pani x.com...[color=blue]
    >
    >
    > Suppose I have some XML document that contains tags of the form
    >
    > <... xmlns:foo="http ://www.bar.org/foo">
    >
    > <... xmlns:foo="baz" >
    >
    > <... xmlns:frobozz=" http://www.bar.org/foo">
    >
    > What's the XPath expression to select the namespace nodes with
    > prefix "foo"?[/color]

    //namespace::*[name()='foo']

    [color=blue]
    > And what's the XPath expression to select the
    > namespace nodes with value "http://www.bar.org/foo"?[/color]

    //namespace::*[.='http://www.bar.org/foo']



    Cheers,

    Dimitre Novatchev [XML MVP],
    FXSL developer, XML Insider,

    http://fxsl.sourceforge.net/ -- the home of FXSL
    Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
    [color=blue]
    >
    > I am using the Perl module XML::XPath. With it, I know how to
    > select namespace nodes with value "baz" (using
    > $xpath_obj->find('//namespace::baz' )), but I get an error if I try
    > to select namespace nodes with value "http://www.bar.org/foo" using
    > $xpath_obj->find('//namespace::http ://www.bar.org/foo'). I can
    > see why the latter would be problematic, since at least the slashes
    > in the namespace's value would collide with XPath syntax, but I
    > don't know how to escape them. (Actually, the parser seems to
    > choke on the colon after http before it gets to do so on the
    > slashes.)
    >
    > With a full implementation of the XML::XPath standard, I suppose
    > I could try something like
    >[/color]
    $xpath_obj->find('namespac e-uri(//namespace::*)=" http://www.bar.org/foo"')),[color=blue]
    > but when I do this I get the error "namespace-uri: Function not
    > supported".
    >
    > I have no clue how to use XML::XPath to select namespace nodes with
    > prefix "foo".
    >
    > Thanks!
    >
    > kj
    >
    > P.S. Is there a "standard" forum for XPath-related queries like
    > this one?
    >
    >
    > --
    > NOTE: In my address everything before the period is backwards.[/color]


    Comment

    • kj

      #3
      Re: XPath: selecting on namespaces

      In <c5dm8b$fe3f$1@ ID-152440.news.uni-berlin.de> "Dimitre Novatchev [MVP XML]" <dnovatchev@yah oo.com> writes:
      [color=blue]
      >"kj" <socyl@987jk.co m> wrote in message
      >news:c5dkrg$ma k$1@reader1.pan ix.com...[color=green]
      >>
      >> Suppose I have some XML document that contains tags of the form
      >>
      >> <... xmlns:foo="http ://www.bar.org/foo">
      >>
      >> <... xmlns:foo="baz" >
      >>
      >> <... xmlns:frobozz=" http://www.bar.org/foo">
      >>
      >> What's the XPath expression to select the namespace nodes with
      >> prefix "foo"?[/color][/color]
      [color=blue]
      >//namespace::*[name()='foo'][/color]
      [color=blue][color=green]
      >> And what's the XPath expression to select the
      >> namespace nodes with value "http://www.bar.org/foo"?[/color][/color]
      [color=blue]
      >//namespace::*[.='http://www.bar.org/foo'][/color]


      Thank you!

      kj

      --
      NOTE: In my address everything before the period is backwards.

      Comment

      Working...