Menu

Evaluating XPath Searches

Help
JLisle
2007-10-30
2012-12-07
  • JLisle

    JLisle - 2007-10-30

    Is there a trick to using the XPath feature with namespaces? Seems if I try mixing default with prefixed namespaces in the search I have no luck. Can XML Copy Editor handle this?

    Ex. XML

    <?xml version="1.0" encoding="UTF-8"?>
    <a xmlns="dummyNamespace" xmlns:other="otherNS">
      <other:b>
        <c>Hello</c>
      </other:b>
    </a>

    XPath Expression              Result

    /                             entire doc from root returned as expected

    /[*name()='a']                entire doc from root returned as expected

    /[*name()='a']/other:b        xpath evaluation issue

     
    • gnschmidt

      gnschmidt - 2007-10-31

      Thanks JLisle, this is definitely possible. Personally I wish namespaces had never been invented - they're far more trouble than use IMO - but as XPath is namespace-aware we'll need to deal with them.

      The problem is that many namespace IDs will conflict with the XPath syntax. For example, I've provided a workaround for xhtml (extract from current wraplibxml.cpp):

          // enable namespace prefixes
          xmlXPathRegisterNs ( context, ( xmlChar * ) "xhtml", ( xmlChar * ) "http://www.w3.org/1999/xhtml" );
          // add others as necessary!

      This means that you can use "//xhtml:b/" to fetch bold elements in XHTML documents.

      I can add as many others as you like, so if you can think of other major vocabularies that would benefit from this, please give me a shout.

      Why not just put them in an XML file that the user can edit? That's another possibility, although this would scatter the user's information a little more widely than I'd like.

      Best,
      Gerald

       
      • Stuart Dootson

        Stuart Dootson - 2008-03-11

        Gerald - I just started using XML Copy Editor & came across the same issue with namespaces - it was one of the first things I tried, as I know that namespaces need a bit of work with XPath...

        Anyway - could you not just harvest namespace prefixes from the XML file & register unambiguous ones with your XPath processor:

        <!-- Namespaces only on root element -->
        <root xmlns:a="something" xmlns:b=".....">...</root>

        would yield prefixes 'a' and 'b' with associated namespaces.

        <!-- Namespaces with conflicting prefixes on root and child elements -->
        <root xmlns:a="...." xmlns:b=".....">
          <child xmlns:b=".....">...</child>
        </root>

        would only yield prefix 'a'.

        You could ignore default namespaces or register a prefix called (say) 'default' for a default namespace (so long as there's only one default namespace declaration).

        Liking the editor, anyway - I wish I'd found it earlier...

        Stuart Dootson

         
    • gnschmidt

      gnschmidt - 2008-03-15

      Thanks Stuart, that's an excellent idea. Should have thought of it really!!!

      V. pressed for development time right now (and there are a couple of known bugs I'm very impatient to fix) but this is obviously the way to go.

      Best,
      Gerald

       
    • HeR

      HeR - 2009-06-11

      Not sure if I'm doing this right but...or if this is the right thread.

      How can I use the XPath Count function in XCE? I use count(//FranchisePublicInformationReport) but get no results found.  So I must be doing something wrong?

      I simply want to know the number, not extract the actual XML fragment(s), of nodes in the given document.  If there is documentation regarding this, please let me know so I can ferret it out.

      Thank you.
      -HER

       
  • Hans Fr. Nordhaug

    studoot: Anyway - could you not just harvest namespace prefixes from the XML file and register unambiguous ones with your XPath processor?

    gnschmidt: Very pressed for development time right now (and there are a couple of known bugs I'm very impatient to fix) but this is obviously the way to go.

    I'm using version 1.2.0.5, but I can't see that this has been fixed/added. At least I'm not able evaluate XPath expression in XML files that uses namespace prefixes. Any comments? Should I submit a feature request?

     
  • gnschmidt

    gnschmidt - 2010-09-24

    Sorry about the endless delays. Work-related development work is making it very difficult for me to keep up with xmlce maintenance at the moment. By all means do submit a request, but I can't give an ETA for the fix unfortunately.
    -Gerald

     

Log in to post a comment.