Xml 在XPath中使用translate函数会导致错误
我必须进行不区分大小写的XML搜索。我已经编写了工作正常的XPath表达式,但是当我在XPath表达式中使用Xml 在XPath中使用translate函数会导致错误,xml,xpath,Xml,Xpath,我必须进行不区分大小写的XML搜索。我已经编写了工作正常的XPath表达式,但是当我在XPath表达式中使用translate函数时,我得到了一个错误。下面是工作正常的XPath表达式: string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string lower = "abcdefghijklmnopqrstuvwxyz"; string xpath = "/data/item[AID/@iskey='true' and AID/text() = 'fe
translate
函数时,我得到了一个错误。下面是工作正常的XPath表达式:
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>
执行此操作时,会出现以下错误:
表达式必须计算为节点集。
这个xpath对我来说很好用
string col="AID";
string xpath = "/data/item[col/@iskey='true' and col/text() = 'fep'][not(*[@iskey][local-name() != 'col' ])]";
现在,我的要求是col值可以是大写或小写,因此我希望在xpath中进行更改,无论xml中存在何种情况的数据,它都会返回结果。
如果用户提供了aid或aid或aid,它会返回结果。不要使用
translate()
,请尝试使用联合:(aid | aid)
新xpath:
/data/item[(aid|AID)/@iskey='true' and (aid|AID)/text() = 'fep'][not(*[@iskey][local-name() != 'AID' and local-name() != 'aid'])]
错误的原因是,在XPath表达式中有如下位置步骤:
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 + "') ])]";
item['aid'/@isKey = 'true' ...]
子表达式:
'aid'/@isKey
在XPath中语法上是非法的,因为/
运算符的左侧必须是一个节点集——这里它只是一个字符串
您希望有这样一个XPath表达式,而不是当前生成的表达式:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
基于XSLT的验证:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
在提供的XML文档上应用此转换时:
<data>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
</data>
<data>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
</data>
fep
太太
计算XPath表达式并将所选节点(本例中仅一个)复制到输出中:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
fep
太太
更新:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
OP很难理解此解决方案。这是一个向他展示该解决方案适用于字符串“aid”的任何资本化的示例:
当上述转换应用于XML文档时,该文档包含的元素名称可以是“aid”的任意大小写,例如:
<data>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
</data>
fep
太太
再次生成预期结果:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
fep
太太
更新2:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
OP仍然不理解该解决方案,并声称对于一个特定的XML文档——如下所示——“我使用了您的表达式,它返回null”
以下是OP评论中提供的XML文档:
<data>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
</data>
<data>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
</data>
fep
前景
当对该文档运行相同的XSLT验证时,再次选择并输出正确的元素:
/data/item
[*[translate(name(), 'aid', 'AID') = 'AID']/@iskey='true'
and
*[translate(name(), 'aid', 'AID') = 'AID'] = 'fep'
]
[not(*[@iskey]
[translate(name(), 'aid', 'AID') != 'AID']
)
]
<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>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>MS</account>
</item>
<item>
<aId iskey="true">fep</aId>
<account>FG</account>
</item>
fep
前景
它不是固定的,可以是Aid,Aid,Aid,Aid等。我想要不区分大小写的xpath。是的。你检查这个表达式了吗?我还是一样error@WaqarJanjua:请参阅我的更新答案——在XSLT中使用时,它可以正确工作。是的,它可以工作。但有一个问题是,将translate函数与“AID”相等,这是固定的。它可以是aid或aid或aid等,因此当我将其更改为“aid”时,表达式返回null。它希望它不区分大小写。@WaqarJanjua:代码是正确的,您需要理解它。在XPath表达式中,“AiD”被转换为“AiD”,这等于“AiD”,因此比较结果为真。处理“援助”时也会发生同样的情况。我建议您也使用不同的输入来运行转换,以确保它在所有情况下都能正常工作。@WaqarJanjua:您最后的评论明确表明您不理解代码。正如我所说的,请尝试对所有可能的源XML文档进行转换,以确保它在任何情况下都能正常工作。