2011-01-14 92 views
6

要首先一個節點,我想問一下,有沒有XML節點下面的兩個語句之間的差異:檢查是否存在使用XSLT

  1. 校驗節點是否是一個空節點;
  2. 檢查節點是否存在;

假設我有一個這樣的XML文件:

<claim_export_xml> 
<claim_export_xml_row> 
    <claim_number>37423</claim_number> 
    <total_submitted_charges>0</total_submitted_charges> 
    <patient_control_no/> 

    <current_onset_date>2009-06-07 00:00:00</current_onset_date> 

,我要檢查 「current_onset_date」 節點是否存在,我用下面的XSLT:

<xsl:for-each select="claim_export_xml_row "> 
     <xsl:if test="claim_number =$mother_claim_no and /current_onset_date "> 

for-each循環是我爲了循環工作必須承擔的一些邏輯。但是在運行這個XSLT之後,我真的得到了錯誤的結果,上面的xml數據不會被我的XSLT抓住。但我不認爲使用「current_onset_date =''」是正確的,因爲它正在測試「current_onset_date是否包含任何內容」。

有沒有人可以告訴我我的錯誤在哪裏,並幫助我列出我的問題一開始,謝謝!

回答

4

應該只是你有兩個「和」的工作,如果你想檢查的空虛,以及你不需要的龍頭/前current_onset_date。

,您可以使用:

<xsl:for-each select="claim_export_xml_row "> 
    <xsl:if test="claim_number =$mother_claim_no and current_onset_date != ''"> 

原因在於,元素的字符串值是其中所有文本的串聯,因此此表達式僅會選擇存在current_onset_date且包含非空字符串的行。如果你想排除包含什麼,但空白的元素,你可以寫:

<xsl:for-each select="claim_export_xml_row "> 
    <xsl:if test="claim_number =$mother_claim_no and normalize-space(current_onset_date) != ''"> 
+0

一旦我擺脫了領先/的,它會抱怨這是無效的xpath和「和」是一個錯字,對不起 – Kevin 2011-01-14 18:25:29

+0

當你刪除/時,你得到的錯誤信息是什麼?我測試了一個類似的結構,它運行良好。 – biziclop 2011-01-14 18:33:19

17

我想問一下,有沒有XML節點以下兩個 語句之間的 區別:

1.檢查節點是否爲空節點;

2.檢查節點是否存在;

這些都需要不同的表達來測試:不存在一個節點不是空節點:

current_onset_date 

這個選擇的當前節點的任何current_onset_date兒童。當且僅當至少有一個這樣的孩子存在時,其布爾值爲true(),否則爲false()

current_onset_date/text() 

這選擇當前節點的任何current_onset_date孩子的任何文本節點子節點。如果沒有,則其布爾值爲false(),否則 - true(),

即使某個元素沒有作爲子元素的文本節點,它仍然可能具有非空字符串值,因爲它可能具有元素作爲後代,其中一些元素後代可能有文本節點子女。

current_onset_date[not(string(.))] 

此選擇任何兒童current_onset_date當前節點的,具有空串(「」)作爲它們的字符串值。這可能適用於「空白元素」。

如果空你的意思是一個元件,它的字符值是空的或白色的,僅空間,則此表達式:

current_onset_date[not(normalize-space())] 

此選擇任何current_onset_date孩子當前節點的,具有空字符串('')或只有空格的字符串作爲它們的字符串值。

有誰告訴我,我的錯誤 是

在您的代碼:

<xsl:for-each select="claim_export_xml_row ">        
    <xsl:if test="claim_number =$mother_claim_no 
           and /current_onset_date ">  

test屬性的表情總是false()因爲/current_onset_date意味着:頂級元素(的該文檔)名爲「current_onset_date」,但您的案例中的頂部元素名爲claim_export_xml

你可能想

claim_number =$mother_claim_no and current_onset_date 

如果你想要的元素是「非空」,則:

claim_number =$mother_claim_no 
    and 
    current_onset_date[normalize-space()]