How to 'select' from XML with namespaces?

后端 未结 3 989
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 18:53

I have an XML document something like :::





        
3条回答
  •  孤城傲影
    2020-12-01 19:11

    Tomalek and ckarras give good answers, but I want to clarify the reasons behind them.

    The elements you aren't matching are in the default namespace of the scope in which they occur in the doc, that is, they are in the namespace declared for that scope without a prefix (e.g.

    xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    

    on the Workbook element). Even though the tagnames lack a namespace prefix, they are in a namespace.

    However, XPath requires that all names of elements in a namespace be qualified with a prefix, or that the namespace be specified explicitly with namespace-uri() in a predicate. Hence, you must either use the local-name() function in a predicate to match the element name (and use the namespace-uri() function as well if there is a danger of name collisions across namespaces), or you must declare each namespace in which you wish to match elements in XPaths with a prefix, and qualify the element names with their namespace prefixes in the XPath expressions.

提交回复
热议问题