xpath translate在java8中似乎无法正常工作
我搜索了很多,想找到确切的等价物,但没能找到。这里的问题是java8-javax.xml.xpath.*的translate方法没有给出预期的输出 范例- 实际的-xpath translate在java8中似乎无法正常工作,java,xpath,case-insensitive,Java,Xpath,Case Insensitive,我搜索了很多,想找到确切的等价物,但没能找到。这里的问题是java8-javax.xml.xpath.*的translate方法没有给出预期的输出 范例- 实际的- XPathExpression xpathitemtype = xpath.compile("//*[@type=\"" + xpathType + "\"]/ancestor::itemtype"); final NodeList itemType = (NodeList) xpathitemtype.evaluate(docum
XPathExpression xpathitemtype = xpath.compile("//*[@type=\"" + xpathType + "\"]/ancestor::itemtype");
final NodeList itemType = (NodeList) xpathitemtype.evaluate(document, XPathConstants.NODESET);
使用翻译-
XPathExpression xpathitemtype = xpath.compile("//*[translate(@type,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" + xpathType.toLowerCase() + "']/ancestor::itemtype");
final NodeList itemType = (NodeList) xpathitemtype.evaluate(document, XPathConstants.NODESET);
现在我使用translate,因为我喜欢获取itemType,而不考虑给定xml文档中的大小写-
如果值为-
exportMedia或exportMedia或exportMedia任何情况下都只需返回输出
在上面的示例中,两个示例返回相同的输出,在我的示例中,translate似乎不起作用
<itemtype code="RemoveCatalogVersionJob">
<attribute qualifier="catalogVersion" type="CatalogVersion">
<persistence type="property"/>
</attribute>
<attribute qualifier="removedItems" type="ExportMEdia">
<persistence type="property"/>
</attribute>
<attribute qualifier="notRemovedItems" type="exportMedia">
<persistence type="property"/>
</attribute>
</itemtype>
现在,如果是exportMedia的任何情况,我需要项值。您的代码工作正常,XPath计算正确。问题是XPath表达式的末尾包含“祖先::itemtype”,并且两个匹配的“attribute”元素只有一个名为“itemtype”的祖先 如果您要更改测试代码
XPathExpression xpathItemType = xpath.compile("//*[translate(@type,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" + xpathType.toLowerCase() + "']/@qualifier");
final NodeList itemType = (NodeList) xpathItemType.evaluate(document, XPathConstants.NODESET);
for(int i = 0; i < itemType.getLength(); i ++) {
System.out.println(itemType.item(i).getNodeValue());
}
。。。显示“ExportMEdia”和“ExportMEdia”都与您的表达式匹配
或者,您可以将输入XML更改为以下内容(只是一个不了解实际方案的示例):
您的代码将两个itemtype与code=“RemoveCatalogVersionJob”和code=“RemoveCatalogVersionJob1”匹配。您的代码工作正常,XPath计算正确。问题是XPath表达式的末尾包含“祖先::itemtype”,并且两个匹配的“attribute”元素只有一个名为“itemtype”的祖先 如果您要更改测试代码
XPathExpression xpathItemType = xpath.compile("//*[translate(@type,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" + xpathType.toLowerCase() + "']/@qualifier");
final NodeList itemType = (NodeList) xpathItemType.evaluate(document, XPathConstants.NODESET);
for(int i = 0; i < itemType.getLength(); i ++) {
System.out.println(itemType.item(i).getNodeValue());
}
。。。显示“ExportMEdia”和“ExportMEdia”都与您的表达式匹配
或者,您可以将输入XML更改为以下内容(只是一个不了解实际方案的示例):
您的代码将使用code=“RemoveCatalogVersionJob”和code=“RemoveCatalogVersionJob1”匹配这两个itemtype
<items>
<itemtype code="RemoveCatalogVersionJob">
<attribute qualifier="notRemovedItems" type="exportMedia">
<persistence type="property"/>
</attribute>
</itemtype>
<itemtype code="RemoveCatalogVersionJob1">
<attribute qualifier="removedItems" type="ExportMEdia">
<persistence type="property"/>
</attribute>
</itemtype>
</items>