xpath匹配-查找是否存在值不在值集中的标记
我有以下XMLxpath匹配-查找是否存在值不在值集中的标记,xpath,Xpath,我有以下XML <Record> <subRecord> <Type>A</Type> </subRecord> </Record> <Record> <subRecord> <Type>B</Type> </subRecord> </Record> <Record> <subRec
<Record>
<subRecord>
<Type>A</Type>
</subRecord>
</Record>
<Record>
<subRecord>
<Type>B</Type>
</subRecord>
</Record>
<Record>
<subRecord>
<Type>C</Type>
</subRecord>
</Record>
A.
B
C
我想查找if not(exists(//Record/subRecord/Type,其中Type不在('A','B'))。通常,在上面的示例中,我应该得到一个false,因为存在一个//Record/subRecord/Type=C。请帮助我为这种情况提供一个xpath。同时使其成为有效的XML片段(根标记): 如果有任何XPATH不是
A
或B
,则该XPATH将返回True
count(Record/subRecord/Type[not(.="A" or .="B")])>0
使用:
not(/*/*/*/Type[not(contains('|A|B|', concat('|',.,'|')))])
false
<t>
<Record>
<subRecord>
<Type>A</Type>
</subRecord>
</Record>
<Record>
<subRecord>
<Type>B</Type>
</subRecord>
</Record>
</t>
此表达式适用于要检查的值列表/集合非常长的情况。它还可以轻松地将管道分隔的值列表作为参数传递
基于XSLT的验证:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"not(/*/*/*/Type[not(contains('|A|B|', concat('|',.,'|')))])"/>
</xsl:template>
</xsl:stylesheet>
true
在此XML文档上应用相同转换时:
not(/*/*/*/Type[not(contains('|A|B|', concat('|',.,'|')))])
false
<t>
<Record>
<subRecord>
<Type>A</Type>
</subRecord>
</Record>
<Record>
<subRecord>
<Type>B</Type>
</subRecord>
</Record>
</t>
说明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"not(/*/*/*/Type[not(contains('|A|B|', concat('|',.,'|')))])"/>
</xsl:template>
</xsl:stylesheet>
true
直接使用非常感谢!我从您的回复中了解到了这个想法,并使用了not(exists(记录/子记录/类型[not(.=“a”或.=“B”)]),它针对我的情况返回false。很高兴提供帮助。欢迎使用堆栈溢出!