XPath on an XML document with namespace

前端 未结 4 1959
清歌不尽
清歌不尽 2020-12-01 17:03

I\'m having this XML document with namespaces and I want to extract some nodes using XPath.

Here\'s the document:



        
相关标签:
4条回答
  • 2020-12-01 17:29

    Had nearly the same problem, I forgot to add the correct namespace for xsi:type (http://www.w3.org/2001/XMLSchema-instance) was using http://www.w3.org/2001/XMLSchema and I did never get any result - now it is working the following way:

    <xsl:value-of select="/item1/item2/item3/@xsi:type"></xsl:value-of>
    
    0 讨论(0)
  • 2020-12-01 17:35

    This way you don't need to specify namespace:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("your xml");
    XmlNode node = xmlDoc.SelectSingleNode("/*[local-name() = 'anyType']");
    XmlNode nodeToImport = xmlDoc2.ImportNode(node, true);
    xmlDoc2.AppendChild(nodeToImport);
    
    0 讨论(0)
  • 2020-12-01 17:37

    I think that

    //anyType[namespace-uri() = "http://www.w3.org/2001/XMLSchema-instance"][local-name() = "type"]
    

    Will do what you want.

    0 讨论(0)
  • 2020-12-01 17:46

    If you are using C# then you need to specify the namespace for the "anyType" element in your XPath:

    var xml = new XmlDocument();
    xml.LoadXml( "your xml" );
    var names = new XmlNamespaceManager( xml.NameTable );
    names.AddNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
    names.AddNamespace( "a", "http://tempuri.org/" );
    var nodes = xml.SelectNodes( "//a:anyType[@xsi:type='Document']", names );
    
    0 讨论(0)
提交回复
热议问题