[英]Locating elements in <li> using selenium webdriver with java
[英]Locating elements with specific namepace using Xpath in Java
我正在嘗試使用Java中的Xpath列出具有特定名稱空間的元素。 我有以下xml文件:
<pd:ProcessDefinition xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pd="http://newsample/cred/process/2007" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<pd:name>Processes/Schemas/GetInline.process</pd:name>
<pd:startName>StartThis</pd:startName>
<pd:startType>
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="param" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</pd:startType>
<pd:endName>End</pd:endName>
<pd:endType>
<xsd:element name="End" type="xsd:string"/>
</pd:endType>
<pd:activity name="MapAnyelement">
<config>
<element>
<xsd:choice>
<xsd:element name="param" type="xsd:string"/>
<xsd:element ref="pfx:param1"/>
</xsd:choice>
</element>
</config>
<pd:coercions>
<pd:coercion xpath="root" element="pfx:param1"/>
</pd:coercions>
<pd:inputBindings/>
</pd:activity>
<pd:activity name="MapComplex">
<config>
<element>
<xsd:element name="mapComplex">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="complex1" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</element>
</config>
<pd:inputBindings>
<mapComplex>
<complex1>
<xsl:value-of select="1"/>
</complex1>
</mapComplex>
</pd:inputBindings>
</pd:activity>
<pd:activity name="MapElement">
<config>
<element>
<xsd:element name="mapElement" type="xsd:string"/>
</element>
</config>
<pd:inputBindings>
<mapElement>
<xsl:value-of select="1"/>
</mapElement>
</pd:inputBindings>
</pd:activity>
</pd:ProcessDefinition>
我正在使用以下代碼查找文件中的任何復雜元素是否具有名稱空間前綴為xs
后代,即它們屬於名稱空間http://www.w3.org/2001/XMLSchema
。
String expression = "/ProcessDefinition/activity|//group/activity|/ProcessDefinition/starter";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
dDoc, XPathConstants.NODESET);
NodeListIterator iter = new NodeListIterator(nodeList);
while(iter.hasNext()) {
Node nNode = iter.next();
Element elem = (Element) nNode;
String aName = elem.getAttribute("name");
String ns = "http://www.w3.org/2001/XMLSchema";
String schemaExpr = "//*[namespace-uri() = '"+ns+"']";
NodeList nN = (NodeList) xPath.compile(schemaExpr).evaluate(nNode, XPathConstants.NODESET);
System.out.println("##### NodeList: " + nN.getLength());
}
但是,代碼始終將NodeList nN
大小返回為“ 0”。 我在這里想念什么?
在XPath中,無限定名稱與沒有名稱空間的元素匹配。 因此, /ProcessDefinition
與您的源文檔中的<pd:ProcessDefinition>
不匹配。
最簡單的解決方法是使用XSLT 2.0+處理器(例如Saxon),然后(1)在靜態上下文中設置defaultXPathNamespace或(2)使用形式為/*:ProcessDefinition/*:activity
。
如果要堅持使用XPath 1.0,則可以編寫/pd:ProcessDefinition/pd:activity
形式的表達式,並設置一個可識別前綴“ pd”的名稱空間上下文。 或者您可以以/*[local-name()='ProcessDefinition']/*[local-name()='activity']/...
的形式編寫表達式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.