Xml 用于检查多个节点的XPath查询,每个节点都有必需的值
我的示例XML如下所示:Xml 用于检查多个节点的XPath查询,每个节点都有必需的值,xml,xpath,Xml,Xpath,我的示例XML如下所示: <FileList> <File> <Name>20120427_1.pdf</Name> <Size>654441</Size> <Property> <Name>Country</Name> <Value>United States</Value> </Property&
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
当答案应该为false时,这与查询在所有节点上查看的结果相同。如何更改它以使其检查每个“文件”节点?若要获取没有国家/地区的文件,请使用
/FileList/File[not(Property[Name='Country'])]
要仅获取其名称,可以使用
/FileList/File[not(Property[Name='Country'])]/Name/text()
要获取没有国家/地区的文件,请使用
/FileList/File[not(Property[Name='Country'])]
要仅获取其名称,可以使用
/FileList/File[not(Property[Name='Country'])]/Name/text()
使用:
not(/*/File[not(Property/Name = 'Country')])
<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:template match="node()|@*">
<xsl:copy-of select=
"not(/*/File[not(Property/Name = 'Country')])"/>
</xsl:template>
</xsl:stylesheet>
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
基于XSLT的验证:
false
此转换:
not(/*/File[not(Property/Name = 'Country')])
<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:template match="node()|@*">
<xsl:copy-of select=
"not(/*/File[not(Property/Name = 'Country')])"/>
</xsl:template>
</xsl:stylesheet>
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
说明:
not(/*/File[not(Property/Name = 'Country')])
<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:template match="node()|@*">
<xsl:copy-of select=
"not(/*/File[not(Property/Name = 'Country')])"/>
</xsl:template>
</xsl:stylesheet>
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
使用使用:
not(/*/File[not(Property/Name = 'Country')])
<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:template match="node()|@*">
<xsl:copy-of select=
"not(/*/File[not(Property/Name = 'Country')])"/>
</xsl:template>
</xsl:stylesheet>
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
基于XSLT的验证:
false
此转换:
not(/*/File[not(Property/Name = 'Country')])
<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:template match="node()|@*">
<xsl:copy-of select=
"not(/*/File[not(Property/Name = 'Country')])"/>
</xsl:template>
</xsl:stylesheet>
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
说明:
not(/*/File[not(Property/Name = 'Country')])
<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:template match="node()|@*">
<xsl:copy-of select=
"not(/*/File[not(Property/Name = 'Country')])"/>
</xsl:template>
</xsl:stylesheet>
<FileList>
<File>
<Name>20120427_1.pdf</Name>
<Size>654441</Size>
<Property>
<Name>Country</Name>
<Value>United States</Value>
</Property>
</File>
<File>
<Name>Name2</Name>
<Size>2147483647</Size>
<Property>
<Name>Name4</Name>
<Value>Value4</Value>
</Property>
<Property>
<Name>Name5</Name>
<Value>Value5</Value>
</Property>
<Property>
<Name>Name6</Name>
<Value>Value6</Value>
</Property>
</File>
<File>
<Name>Name3</Name>
<Size>2147483647</Size>
<Property>
<Name>Name7</Name>
<Value>Value7</Value>
</Property>
<Property>
<Name>Name8</Name>
<Value>Value8</Value>
</Property>
<Property>
<Name>Country</Name>
<Value>UK</Value>
</Property>
</File>
</FileList>
使用