在 XPath 中使用 translate 函数会出错 [英] Using translate function in XPath gives error
问题描述
我必须进行不区分大小写的 XML 搜索.我已经编写了工作正常的 XPath 表达式,但是当我在 XPath 表达式中使用 translate
函数时,我收到一个错误.下面是运行良好的 XPath 表达式:
I have to do a case-insensitive XML search. I've written XPath expressions which are working fine, but when I use the translate
function inside an XPath expression I get an error. Below are the XPath expressions that are working fine:
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lower = "abcdefghijklmnopqrstuvwxyz";
string xpath = "/data/item[AID/@iskey='true' and AID/text() = 'fep'][not(*[@iskey][local-name() != 'AID' ])]";
string xpath1 = "/data/item[translate('Aid','" + upper + "','" + lower + "')]";
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<data>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
</data>
我想像这样将 translate
函数放在第一个 XPath 表达式中:
I want to place the translate
function in the first XPath expression like this:
string xpath = "/data/item[translate('AID','" + upper + "','" + lower + "')/@iskey='true' and translate('AID','" + upper + "','" + lower + "')/text() = 'fep'][not(*[@iskey][local-name() != translate('aid','" + upper + "','" + lower + "') ])]";
执行此操作时,出现以下错误:
When I do this, I get the following error:
表达式必须计算为节点集.
这个 xpath 对我来说很好用.
This xpath is working fine for me.
string col="AID";
string xpath = "/data/item[col/@iskey='true' and col/text() = 'fep'][not(*[@iskey][local-name() != 'col' ])]";
现在我的要求是 col 值可以是任何情况下的大写或小写,所以我想改变我的 xpath,无论在哪种情况下数据存在于 xml 中,它都会返回结果.如果用户提供援助或援助或援助,它会将结果返回给我.
Now my requirement is that the col value can be in any case upper case or lower case, so I want a change in my xpath with which it returns me the result irrespective of in which case data is present in the xml. If a user gives aid or Aid or aiD it returns me the result.
推荐答案
该错误是由于在您的 XPath 表达式中您有这样的位置步骤:
The error is due to the fact that in your XPath expression you have a location step like this:
item['aid'/@isKey = 'true' ...]
子表达式:
'aid'/@isKey
在 XPath 中在语法上是非法的,因为 /
运算符的左侧必须是一个节点集——这里它只是一个字符串.
is syntactically illegal in XPath, because the left-hand-side of the /
operator must be a node-set -- here it is just a string.
您想要一个这样的 XPath 表达式 -- 而不是您目前正在生成的那个:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
基于 XSLT 的验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vLower" select="'aid'"/>
<xsl:variable name="vUpper" select="'AID'"/>
<xsl:template match="node()|@*">
<xsl:copy-of select=
"/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<data>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
</data>
计算 XPath 表达式并将所选节点(在本例中只有一个)复制到输出:
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
更新:
OP 难以理解此解决方案.这是一个示例,向他展示该解决方案适用于字符串aid"的任何大写形式:
The OP has difficulty understanding this solution. This is an example to show him that the solution works for any capitalisation of the string "aid":
当上述转换应用于 XML 文档时,该文档包含的元素名称为aid"的任意大写——例如:
When the transformation above is applied on an XML document, that contains element names that are any capitalization of "aid" -- such as this:
<data>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
</data>
再次产生预期结果:
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
更新 2:
OP 仍然不理解解决方案,并声称对于特定的 XML 文档——下面提供——我使用你的表达式它返回 null"
The OP still doesn't understand the solution and claims that for a specific XML document -- provided below -- "I used your expression it returns null "
这是 OP 评论中提供的 XML 文档:
<data>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
</data>
当针对此文档运行相同的 XSLT 验证时,再次选择并输出正确的元素:
When the same XSLT verification is run against this document, again the correct element is selected and output:
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
这篇关于在 XPath 中使用 translate 函数会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!