2

Given the following:
You will note that embedded in this xml sample is an Organisation node with its' own namespace declared with xmlns=""

This is declared like this rather than using a prefix. Is there any way of finding all the namespaces declared in an xml document using xpath?

I have tried a few variations, unfortunately xmlns is not treated like any other attributes of a document (for obvious reasons) and I cannot find a way to get them out using xpath.

//@* does not include them for instance.

I need a list of all the xmlns attributes in the document.

<rss version="2.0" xmlns:a10="http://www.w3.org/2005/Atom">
  <channel xmlns:media="http://search.yahoo.com/mrss">
    <title>Search Results Title</title>
    <link>http://acme.co.za/searchResults</link>
    <description>Search Results Descrption</description>
    <item>
      <guid isPermaLink="false">uuid:169d3229-3b99-4647-9791-492bbbf4c419/PGS0900.PDF</guid>
      <link>link</link>
      <title>Title</title>
      <description>This page...</description>
      <media:thumbnail url="..." width="100" height="100"></media:thumbnail>
      <Organisation xmlns="http://acme.co.za/MYNAMESPACE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Name>Volkswagen</Name>
        <Business>Kraft-durch-Freude-Wagen</Business>
        <Type>Public</Type>
        <RegistrationNumber>5908213224080</RegistrationNumber>
      </Organisation>
    </item>
  </channel>
</rss>

2 Answers 2

1

You should be able to do something like //namespace:*

However, assuming this works, it will give you a list of namespace nodes, in document order, with a lot of duplicates. You'd probably be better off simply traversing the document via DOM, accumulating the set (no dupes) of namespaces. Note that you need to check attributes as well as elements.

0

The correct syntax is //namespace-uri()

That gives you all of the namespaces in the document.

Regards Craig.

1
  • //*/namespace-uri() shows only the namespaces of elements; however, both of these expressions will return duplicates.
    – xcut
    Dec 18, 2009 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.