XPath and namespaces...

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

    XPath and namespaces...

    Hi,
    I've got a little bit of a problem when dealing with namespaces and XPath.

    I'm trying very basic things, like showing all the nodes of one particular
    namespace. Here is my XPath statement:
    //*[local-name() = 'buch' and namespace-uri() =
    'http://www.example.com/buecher']

    Unfortunately this doesn't work. I have found several ways to solve this
    with XSLT, but I need the pure XPath statement.

    My XML file looks like this:

    <?xml version="1.0"?>
    <!-- Dateiname: Sammlung.xml -->
    <SAMMLUNG
    xmlns:buch="htt p://www.example.com/buecher"
    xmlns:cd="http://www.example.com/cds">

    <buch:ARTIKEL Status="vorhand en">
    <buch:TITEL>T he Adventures of Huckleberry Finn</buch:TITEL>
    <buch:AUTOR>Mar k Twain</buch:AUTOR>
    <buch:PREIS>12. 75</buch:PREIS>
    </buch:ARTIKEL>
    <cd:ARTIKEL>
    <cd:TITEL>Violi nkonzert D-Dur</cd:TITEL>
    <cd:KOMPONIST>B eethoven</cd:KOMPONIST>
    <cd:PREIS>14.95 </cd:PREIS>
    </cd:ARTIKEL>
    </SAMMLUNG>


    Could anyone please give me a hint how to solve this problem?

    Thanks,
    Stefan


  • Martin Honnen

    #2
    Re: XPath and namespaces...



    Stefan Franke wrote:

    [color=blue]
    > I've got a little bit of a problem when dealing with namespaces and XPath.
    >
    > I'm trying very basic things, like showing all the nodes of one particular
    > namespace. Here is my XPath statement:
    > //*[local-name() = 'buch' and namespace-uri() =
    > 'http://www.example.com/buecher']
    >
    > Unfortunately this doesn't work.[/color]

    What do you mean by "doesn't work"? Do you get an error? Which software
    are you using to test that XPath expression?

    [color=blue]
    > My XML file looks like this:
    >
    > <?xml version="1.0"?>
    > <!-- Dateiname: Sammlung.xml -->
    > <SAMMLUNG
    > xmlns:buch="htt p://www.example.com/buecher"
    > xmlns:cd="http://www.example.com/cds">
    >
    > <buch:ARTIKEL Status="vorhand en">
    > <buch:TITEL>T he Adventures of Huckleberry Finn</buch:TITEL>
    > <buch:AUTOR>Mar k Twain</buch:AUTOR>
    > <buch:PREIS>12. 75</buch:PREIS>
    > </buch:ARTIKEL>
    > <cd:ARTIKEL>
    > <cd:TITEL>Violi nkonzert D-Dur</cd:TITEL>
    > <cd:KOMPONIST>B eethoven</cd:KOMPONIST>
    > <cd:PREIS>14.95 </cd:PREIS>
    > </cd:ARTIKEL>
    > </SAMMLUNG>[/color]

    Well looking at your example XML the expression

    //*[local-name() = 'buch' and namespace-uri() =
    'http://www.example.com/buecher']

    will not find any element as the XML doesn't contain any
    <buch xmlns="http://www.example.com/buecher" />
    elements, perhaps you are looking for

    //*[local-name() = 'ARTIKEL' and namespace-uri() =
    'http://www.example.com/buecher']


    --

    Martin Honnen

    Comment

    • Stefan Franke

      #3
      Re: XPath and namespaces...

      > perhaps you are looking for[color=blue]
      >
      > //*[local-name() = 'ARTIKEL' and namespace-uri() =
      > 'http://www.example.com/buecher']
      >[/color]

      oops, indeed I am.

      Thanks very much,
      Stefan


      Comment

      • Stefan Franke

        #4
        Re:still problems with XPath and namespaces...

        Hi,
        I'm still having troubles with XPath and namespaces.

        The XML I'm using looks like this:

        <SAMMLUNG
        xmlns:buch="htt p://www.example.com/buecher"
        xmlns:cd="http://www.example.com/cds">

        <buch:ARTIKEL Status="vorhand en">
        <buch:TITEL>T he Marble Faun</buch:TITEL>
        <buch:AUTOR>Nat haniel Hawthorne</buch:AUTOR>
        <buch:PREIS>10. 95</buch:PREIS>
        </buch:ARTIKEL>
        <cd:ARTIKEL>
        <cd:TITEL>Violi nkonzerte 1, 2 und 3</cd:TITEL>
        <cd:KOMPONIST>M ozart</cd:KOMPONIST>
        <cd:PREIS>16.49 </cd:PREIS>
        </cd:ARTIKEL>
        </SAMMLUNG>


        If I want to display e.g. all the CDs of Mozart, I would do something like
        this if I wouldn't have to deal with namespaces:

        string(//*/TITEL[following-sibling::KOMPON IST='Mozart'])

        My suggestion when dealing with namespaces would be something like this (but
        it doesn't work, as there is no TITEL-element anywhere, but I don't know how
        to get it in there):

        string(//*[local-name()='ARTIKEL ' and
        namespace-uri()='http://www.example.com/cds']/*[string(local-name()='KOMPONI ST')
        = 'Mozart'])

        Anyway, as I said before, that doesn't work. Could someone please help me
        with that?

        Thanks,
        Stefan


        Comment

        • Martin Honnen

          #5
          Re: still problems with XPath and namespaces...



          Stefan Franke wrote:

          [color=blue]
          > The XML I'm using looks like this:
          >
          > <SAMMLUNG
          > xmlns:buch="htt p://www.example.com/buecher"
          > xmlns:cd="http://www.example.com/cds">
          >
          > <buch:ARTIKEL Status="vorhand en">
          > <buch:TITEL>T he Marble Faun</buch:TITEL>
          > <buch:AUTOR>Nat haniel Hawthorne</buch:AUTOR>
          > <buch:PREIS>10. 95</buch:PREIS>
          > </buch:ARTIKEL>
          > <cd:ARTIKEL>
          > <cd:TITEL>Violi nkonzerte 1, 2 und 3</cd:TITEL>
          > <cd:KOMPONIST>M ozart</cd:KOMPONIST>
          > <cd:PREIS>16.49 </cd:PREIS>
          > </cd:ARTIKEL>
          > </SAMMLUNG>
          >
          >
          > If I want to display e.g. all the CDs of Mozart, I would do something like
          > this if I wouldn't have to deal with namespaces:
          >
          > string(//*/TITEL[following-sibling::KOMPON IST='Mozart'])[/color]

          Not really, if you apply the string function to a nodeset then you get
          the string value of the first node in document order so the above
          doesn't give you all CD titles but only one.
          [color=blue]
          > My suggestion when dealing with namespaces would be something like this (but
          > it doesn't work, as there is no TITEL-element anywhere, but I don't know how
          > to get it in there):
          >
          > string(//*[local-name()='ARTIKEL ' and
          > namespace-uri()='http://www.example.com/cds']/*[string(local-name()='KOMPONI ST')
          > = 'Mozart'])[/color]

          How are you evaluating your XPaths? You should simply be able to declare
          prefixes for the namespaces and then use e.g.
          //prefix:TITLE
          instead of going to the pain of using namespace-uri comparisons.
          So I think you should better tell us about the software you are using to
          evaluate XPaths and then probably someone can tell you how to use
          namespace prefixes bound to namespaces when evaluating XPath expressions.

          --

          Martin Honnen

          Comment

          • Stefan Franke

            #6
            Re: still problems with XPath and namespaces...

            > How are you evaluating your XPaths? You should simply be able to declare[color=blue]
            > prefixes for the namespaces and then use e.g.
            > //prefix:TITLE[/color]


            Hi Martin,
            I'm using FiveSight's XPathTester (Version 1.4 for Saxon - 3 December 2001).
            When I try something like "//*/cd:TITEL" an error message comes up and tells
            me that 'Prefix cd has not been declared'. So I thought, oh well, I have to
            find a way around this problem. Am I wrong in thinking that?

            regards,
            Stefan


            Comment

            • Martin Honnen

              #7
              Re: still problems with XPath and namespaces...



              Stefan Franke wrote:

              [color=blue]
              > I'm using FiveSight's XPathTester (Version 1.4 for Saxon - 3 December 2001).
              > When I try something like "//*/cd:TITEL" an error message comes up and tells
              > me that 'Prefix cd has not been declared'. So I thought, oh well, I have to
              > find a way around this problem. Am I wrong in thinking that?[/color]

              No, you need to bind that prefix to the proper namespace but an XPath
              tool should give you a way to do that before evaluating an expression.
              I don't know FiveSight's XPathTester and it seems it is no longer
              available on their web site.
              Saxon 6.5 as documented here
              <http://saxon.sourcefor ge.net/saxon6.5.3/api-guide.html#Expr essions>
              has a method
              declareNamespac e
              in its XPath API so with Java and Saxon it should be possible to bind
              prfixes to namespaces before evaluating an expression.



              --

              Martin Honnen

              Comment

              Working...