1

我在尝试使用 Xpath 解析 XML 内容时遇到了困难。XML 包含命名空间信息。我试图创建一个 NameSpaceContextImp(jdk 中 NameSpaceContext 接口的 apache WS commons 实现)来将 namaspace 前缀映射到 URI,但是无法成功查询 xml 文档。当我在http://chris.photobooks.com/xml/default.htm使用在线 xpath 测试工具时,我使用的 xpath 查询带有我预期的节点/元素。所以我想弄清楚我做错了什么。我正在提供 xml 文档和示例代码片段。我将不胜感激任何反馈。需要注意的是,我尝试过使用和不使用命名空间前缀的 xpath 查询。

NamespaceContextImpl namespaceContext = new NamespaceContextImpl();
namespaceContext.startPrefixMapping("wsp", "http://schemas.xmlsoap.org/ws/2002/12/policy");
namespaceContext.startPrefixMapping("L7p", "http://www.layer7tech.com/ws/policy");
String policyXml = "xml content that is pasted below"
InputSource inputSource = new InputSource(new StringReader(policyXml));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xPath = xpathFactory.newXPath();
xPath.setNamespaceContext(namespaceContext);
XPathExpression xpathExpression = xPath.compile("/wsp:Policy/wsp:All");
String evaluation = xpathExpression.evaluate(inputSource);
if (evaluation.trim().length() > 0) {
    System.out.println(evaluation);
}


    <?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
    <wsp:All wsp:Usage="Required">
        <L7p:SetVariable>
            <L7p:AssertionComment assertionComment="included">
                <L7p:Properties mapValue="included">
                    <L7p:entry>
                        <L7p:key stringValue="RIGHT.COMMENT"/>
                        <L7p:value stringValue="Used to enable message logging, Null (allow MSGDEBUG Header to set level), 0 - default,1 - Medium,2 - Full"/>
                    </L7p:entry>
                </L7p:Properties>
            </L7p:AssertionComment>
            <L7p:Base64Expression stringValue=""/>
            <L7p:VariableToSet stringValue="LOCAL_POLICY_DEBUG_LEVEL"/>
        </L7p:SetVariable>
        <L7p:Include>
            <L7p:PolicyGuid stringValue="ec1f4166-4299-4e44-bf9d-c5c2a9f0c894"/>
        </L7p:Include>
        <L7p:SslAssertion>
            <L7p:Option optionValue="Optional"/>
        </L7p:SslAssertion>
        <wsp:OneOrMore L7p:Enabled="false" wsp:Usage="Required">
            <L7p:SpecificUser>
                <L7p:Enabled booleanValue="false"/>
                <L7p:IdentityProviderOid longValue="-2"/>
                <L7p:UserLogin stringValue="test"/>
                <L7p:UserName stringValue="test"/>
                <L7p:UserUid stringValue="58916874"/>
            </L7p:SpecificUser>
            <L7p:SpecificUser>
                <L7p:Enabled booleanValue="false"/>
                <L7p:IdentityProviderOid longValue="-2"/>
                <L7p:UserLogin stringValue="test"/>
                <L7p:UserName stringValue="test"/>
                <L7p:UserUid stringValue="58916873"/>
            </L7p:SpecificUser>
            <L7p:SpecificUser>
                <L7p:Enabled booleanValue="false"/>
                <L7p:IdentityProviderOid longValue="-2"/>
                <L7p:UserLogin stringValue="test"/>
                <L7p:UserName stringValue="test"/>
                <L7p:UserUid stringValue="58916876"/>
            </L7p:SpecificUser>
            <L7p:SpecificUser>
                <L7p:Enabled booleanValue="false"/>
                <L7p:IdentityProviderOid longValue="-2"/>
                <L7p:UserLogin stringValue="test"/>
                <L7p:UserName stringValue="test"/>
                <L7p:UserUid stringValue="58916875"/>
            </L7p:SpecificUser>
            <L7p:SpecificUser>
                <L7p:Enabled booleanValue="false"/>
                <L7p:IdentityProviderOid longValue="-2"/>
                <L7p:UserLogin stringValue="testengineering-user"/>
                <L7p:UserName stringValue="testengineering-user"/>
                <L7p:UserUid stringValue="48201728"/>
            </L7p:SpecificUser>
        </wsp:OneOrMore>
        <wsp:OneOrMore wsp:Usage="Required">
            <L7p:HttpRoutingAssertion>
                <L7p:ProtectedServiceUrl stringValue="http://localhost:13000/Services/Finance/v1"/>
                <L7p:RequestHeaderRules httpPassthroughRuleSet="included">
                    <L7p:Rules httpPassthroughRules="included">
                        <L7p:item httpPassthroughRule="included">
                            <L7p:Name stringValue="Cookie"/>
                        </L7p:item>
                        <L7p:item httpPassthroughRule="included">
                            <L7p:Name stringValue="SOAPAction"/>
                        </L7p:item>
                    </L7p:Rules>
                </L7p:RequestHeaderRules>
                <L7p:RequestParamRules httpPassthroughRuleSet="included">
                    <L7p:ForwardAll booleanValue="true"/>
                    <L7p:Rules httpPassthroughRules="included"/>
                </L7p:RequestParamRules>
                <L7p:ResponseHeaderRules httpPassthroughRuleSet="included">
                    <L7p:Rules httpPassthroughRules="included">
                        <L7p:item httpPassthroughRule="included">
                            <L7p:Name stringValue="Set-Cookie"/>
                        </L7p:item>
                    </L7p:Rules>
                </L7p:ResponseHeaderRules>
            </L7p:HttpRoutingAssertion>
            <L7p:Include>
                <L7p:PolicyGuid stringValue="b438384e-eeb0-45c5-8a7e-d30da78f07ee"/>
            </L7p:Include>
        </wsp:OneOrMore>
    </wsp:All>
</wsp:Policy>
4

2 回答 2

1

元素下唯一的 CDATAAll是空白,您可以对其进行修剪。如果要获取 DOM 元素,请使用NODENODESET ( NodeList) 选项。

String xml = "<foo><bar baz='hello' /></foo>";
InputSource src = new InputSource(new StringReader(xml));
XPathExpression expr = XPathFactory.newInstance().newXPath()
    .compile("/foo/bar");
Node node = (Node) expr.evaluate(src, XPathConstants.NODE);

Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.transform(new DOMSource(node), new StreamResult(System.out));
于 2012-06-05T20:41:47.043 回答
0

使用一个参数调用 evaluate() 请求将结果转换为字符串,这相当于获取请求节点的字符串值:即所选元素的文本节点后代的串联。由于所有后代文本节点都是空白,因此您将一无所获。

这一次,命名空间不是问题。

于 2012-06-05T22:33:47.107 回答