dongyin2390 2014-03-11 14:41
浏览 58

嵌套命名空间:如何检索内部节点

I have this scenario, returned by a SOAP WS

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:myFunctionEnvelopeName xsi:type="xsd:string" xmlns:ns1="http://fooNameSpace.comm">
            <myFunctionName xmlns="http://barNameSpace.com">
                [some nodes here]
            </myFunctionName>
        </ns1:myFunctionEnvelopeName>
    </soapenv:Body>
</soapenv:Envelope>

I want to select subnodes child of myFunctionName but I have some issues

  • If i register ns1 namespace into my XPath I'm able only to select myFunctionEnevelopeName (even if I try with getElementsByTagName() I receive back 0 nodes
  • If I register, let's say, ns2 with barNameSpace.com, my query will not return my elements
    • If I don't register any namespace my query will not return my elements

Only workaround I have found is to

  • Register ns1 as namespace of XPath
  • Retrieve "main node" (myFunctionEnevelopeName)
  • Extract textContent from "main node" (that of course is a valid xml)
  • Create a brand-new Xpath with the text content obtained
  • Register ns2 as namespace of brand-new Xpath
  • Obtain what I want

I'm sure that exists a clever method to do that, but maybe I don't know it. Someone could give me pointers?

  • 写回答

1条回答 默认 最新

  • duan2477 2014-03-11 15:48
    关注

    XPath itself doesn't prevent you from registering multiple namespaces and you can therefore use several prefixes in an XPath expression. For example /ns1:element-1/ns2:element-2. Using no prefix at all is the same as matching elements that don't have a namespace.

    DOM API has the method getElementsByTagNameNS() for matching elements with a namespace.

    评论

报告相同问题?

悬赏问题

  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo