2012-11-07 24 views
1

我的示例XML如下:XPath查詢以檢查多個節點各自具有所需的值

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

要求是,每個文件節點具有在屬性格式/ Name元素一個國家的值。到目前爲止,我已經創建了一個XPath查詢:

/FileList/File/Property/Name/text()='Country' 

此作爲傳遞查詢作爲真正在看的時候,答案應該是假的所有節點。我如何改變它,以便它檢查每個「文件」節點?

回答

1

使用

not(/*/File[not(Property/Name = 'Country')]) 

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:template match="node()|@*"> 
    <xsl:copy-of select= 
    "not(/*/File[not(Property/Name = 'Country')])"/> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的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> 

評估XPath表達式,併產生想要的,正確的結果:

false 

說明

使用的double negation law

0

獲取文件沒有一個國家,使用

/FileList/File[not(Property[Name='Country'])] 

只得到它的名字,就可以使用

/FileList/File[not(Property[Name='Country'])]/Name/text()