3

I'd like to be able to evaluate normal XPath expressions against the following XML which has a pesky xmlns attribute that I don't care about:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="AuthorIT.xslt"?>
<AuthorIT version="6.0.8" xmlns="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
    <Objects>
        <Book>
            <Object>
                <CreatedBy>Me</CreatedBy>
                <CreatedDate>2012-11-20T12:35:33</CreatedDate>
                <Description>String I want to get</Description>
                <FolderID>12345</FolderID>
                <GUID>abcdefg1234567abcdefg1234567abcd</GUID>
                <ID>99999</ID>
            </Object>
        </Book>
    </Objects>
</AuthorIT>

In XMLSpy (or this free web tool) the expression to the "String I want to get" is as follows:

/AuthorIT/Objects/Book/Object/Description

This is how I'm specifying the namespace in c#:

XPathDocument document = new XPathDocument("/path/to/my/file");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("abc", "http://www.authorit.com/xml/authorit");

Once I've specified the namespace, here's how I have to amend the perfectly-good XPath expression to get the same result:

/abc:AuthorIT/abc:Objects/abc:Book/abc:Object/abc:Description

Horrible, ain't it?! Is there a way to evaluate XPath expressions that bypass and totally ignore the presence of this extremely pesky "xmlns" attribute on the AuthorIT root element? If I totally strip the root element so that it's just by itself, I don't need to specify a namespace and I can use normal expressions. Is there a way to tell .NET to just work as if the "xmlns" rubbish wasn't there in the first place?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Rainier Wolfcastle
  • 606
  • 2
  • 7
  • 18

1 Answers1

1

Yes, there is a way its to use/local-name()='AuthorIT'/local-name()='Objects'/local-name()='Book'/local-name()='Object'/local-name()='Description'

Rahul
  • 170
  • 2
  • 14
  • kindly accept answer if it worked for you.So that its helpful for users who see it later – Rahul Aug 14 '13 at 04:07
  • Um. I don't meant to sound ungrateful but that's hardly any better than the "abc:" prefix. If it comes down to it I'll just strip all the attributes from the root element before evaluating XPath expressions. It might be dodgy but at least my XPaths will be normal and readable. – Rainier Wolfcastle Aug 14 '13 at 04:25
  • But in situations where you may have to rely on xsds given by some client you cant control them to change the name prefix to a normal node.I think this is helpful in those situations – Rahul Aug 14 '13 at 06:47
  • I think I've reluctantly had to come to the realisation that this is just the way it is. How sad that we live in a world where the presence of a default namespace in an XML file means I have to qualify all my QNames in an XPath expression. To my way of thinking anything without a namespace prefix SHOULD be addressable "as is" without any qualification. Who dreams up these mis-features, anyway? This has to be a crime against all developerdom! – Rainier Wolfcastle Aug 14 '13 at 22:34
  • I have actually gone with stripping the default xmlns="rubbish" because the mere presence of this is polluting all HTML tags that are present as values (not part of the XML structure) in other nodes. Every single

    tag, etc, is getting an xmlns="rubbish" attribute, which I would consider to be a very nasty bug/side-effect. Perhaps it's the .NET Framework, I don't know. All my problems have now gone, however, just by removing this pointless attribute from the root node :-)

    – Rainier Wolfcastle Aug 16 '13 at 01:26