Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 用于检查多个节点的XPath查询,每个节点都有必需的值_Xml_Xpath - Fatal编程技术网

Xml 用于检查多个节点的XPath查询,每个节点都有必需的值

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&

我的示例XML如下所示:

<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>
使用