2013-03-22 37 views
3

有沒有設定值的值標籤我有下面的XMLXPath的比賽 - 如果發現存在

<Record> 
    <subRecord> 
     <Type>A</Type> 
    </subRecord> 
</Record> 
<Record> 
    <subRecord> 
     <Type>B</Type> 
    </subRecord> 
</Record> 
<Record> 
    <subRecord> 
     <Type>C</Type> 
    </subRecord> 
</Record> 

我想找到如果不是(存在(//記錄/子記錄/類型,其中在鍵入不( 'A','B'))。通常在上面的例子中,我應該得到一個假,因爲有一個// record/subRecord/Type = C,請幫助我用這個條件的xpath

回答

2

這是一個有效的XML片段(根標籤):

<Data> 
    <Record> 
     <subRecord> 
      <Type>A</Type> 
     </subRecord> 
    </Record> 
    <Record> 
     <subRecord> 
      <Type>B</Type> 
     </subRecord> 
    </Record> 
    <Record> 
     <subRecord> 
      <Type>C</Type> 
     </subRecord> 
    </Record> 
</Data> 

這XPATH將返回<Type>C</Type>

Record/subRecord/Type[not(.="A" or .="B")] 

如果有任何不屬於AB這個XPATH將返回true。

count(Record/subRecord/Type[not(.="A" or .="B")])>0 
+0

謝謝!我從你的迴應中得到了這個想法,並且不使用(存在(Record/subRecord/Type [not(。=「A」或。=「B」)])),這對我的狀況返回false。 – 2013-03-22 10:08:21

+0

很高興幫助。歡迎來到堆棧溢出! – MattH 2013-03-22 10:10:24

0

用途:

not(/*/*/*/Type[not(contains('|A|B|', concat('|',.,'|')))]) 

這種表達是可行的情況下使用其列表/套針對其檢查值是很長。它還可以輕鬆地將管道分隔的值列表作爲參數傳遞。

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> 

當在下面的XML文檔(通過包裝提供片段到單個頂部元件形成)被施加這種轉變:

<t> 
    <Record> 
     <subRecord> 
      <Type>A</Type> 
     </subRecord> 
    </Record> 
    <Record> 
     <subRecord> 
      <Type>B</Type> 
     </subRecord> 
    </Record> 
    <Record> 
     <subRecord> 
      <Type>C</Type> 
     </subRecord> 
    </Record> 
</t> 

評估XPath表達式並將此評估的結果複製到輸出中:

false 

當相同的變換上應用這個 XML文檔:

<t> 
    <Record> 
     <subRecord> 
      <Type>A</Type> 
     </subRecord> 
    </Record> 
    <Record> 
     <subRecord> 
      <Type>B</Type> 
     </subRecord> 
    </Record> 
</t> 

再次正確的結果產生

true 

說明

直接使用Double Negation Law