Hi I’m trying to get a variable value (ref within contractRef) in the xml below, but when I use what I assume to be the xpath:
/discovery/contractRef[@xmlns='http://schemas.xmlsoap.org/disco/scl/']/@ref
it returns nothing. How can I get this variable, what am I missing? Thanks
<?xml version="1.0" encoding="utf-8"?>
<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
<contractRef ref="http://127.0.0.1/Services/Core/Calendar/LBCalendar.svc?wsdl" docRef="http://127.0.0.1/Services/Core/Calendar/LBCalendar.svc" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
</discovery>
This is a FAQ. The element
<discovery>is in the namespacehttp://schemas.xmlsoap.org/disco/and the child element<contractRef>is in the namespacehttp://schemas.xmlsoap.org/disco/scl/You need to register these namespaces with some prefix and then use those prefixes in your XPath expression. How “registering” is done, depends on the language/environment where the XPath expression is used. If the XPath is used inside an XML document, then prefixes are “registered” just by declaring a prefixed namespace, for examplexmlns:scl="http://schemas.xmlsoap.org/disco/scl/"Let’s assume that
http://schemas.xmlsoap.org/disco/is registered to prefixdandhttp://schemas.xmlsoap.org/disco/scl/is registered to prefixsclthen a correct XPath expression would beNamespaces are a fundamental feature in XML and XPath. Please take time to learn them. A hack to avoid using namespace prefixes in XPath expressions is to use
local-name()functionNote that the attribute name doesn’t need a namespace prefix since it belongs to no-namespace.