无法使用 libxml2 处理 xpath 查询

Not able to process xpath query using libxml2

本文关键字:处理 xpath 查询 libxml2      更新时间:2023-10-16

我在下面提到了xml路径查询

//Namespace 字符* pszNs_Soap_prefix = "s"; 字符* pszNs_Soap_URL = "http://schemas.xmlsoap.org/soap/envelope/";

字符* pszNs_SamlP_Prefix = "sp"; char* pszNS_SamlP_URL = "urn:oasis:names:tc:SAML:1.0:protocol";

字符* pszNS_Saml_Prefix = "sl"; char* pszNS_Saml_URL = "urn:oasis:names:tc:SAML:1.0:assertion";

char* pszXPathQuery3 = "/s:Envelope/s:Body/sp:Response/sl:Assertion/sl:AttributeStatement/s1:Attribute[@AttributeName='role']/sl:AttributeValue";

但是从xmlXPathEvalExpression库函数中获取NULL,上面是我的xpath查询,不知道我哪里出错了。

m_pXmlXPathObject=xmlXPathEvalExpression((xmlChar*(pszXPathQuery,m_pXmlXPathContext(;

XML 字符串

<saml:Assertion MajorVersion="1"
MinorVersion="1"
AssertionID="_a75adf55-01d7-40cc-929f-dbd8372ebdfc"
Issuer="SAML_AUTH_URI"
IssueInstant="2005-06-08T12:00:01.100Z">
<saml:AuthenticationStatement
AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:password"
AuthenticationInstant="2005-06-08T11:59:59.000Z">
<saml:Subject>
<saml:NameIdentifier>jre</saml:NameIdentifier>
</saml:Subject>
</saml:AuthenticationStatement>
<saml:AttributeStatement>
<saml:Attribute
AttributeName="role"
AttributeNamespace="oasis:names:tc:xacml:2.0:subject">
<saml:AttributeValue>weboamadmin</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
</saml:Assertion>
</samlp:Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

问题是您需要在执行 xpath 查询之前注册命名空间。 xmlsoft.org 网站提供了一个关于如何执行此操作的很好的例子!

http://xmlsoft.org/examples/xpath1.c