问题描述
首先,我想问一下,对于XML节点,以下两种说法是否有区别:
To begin with, I would like to ask, is there a difference between the following two statements for XML nodes:
- 检查节点是否为空节点;
- 检查节点是否存在;
假设我有一个这样的 XML 文件:
Suppose I have an XML file like this:
<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:
and I want to check whether the "current_onset_date" node exists or not, I used the following 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 是否包含任何内容".
The for-each loop is some logic that I have to bear with in order for the loop to work. But I actuall got wrong result after running this XSLT, the above xml data won't be grabbed by my XSLT. But I don't think using "current_onset_date =‘‘ " is correct either, since it is testing for "whether current_onset_date contains nothing".
谁能告诉我我的错误在哪里,并帮助我解决开头列出的问题,谢谢!
Could anybody tell me where my mistake is and also help me with my question listed in the beginning, thanks!
推荐答案
它应该可以工作,只是你有两个和"并且你不需要在 current_onset_date 之前的前导/.
It should work except that you've got two "and"'s and you don't need the leading / before current_onset_date.
如果您还想检查空性,可以使用:
If you want to check for emptiness as well, you can use:
<xsl:for-each select="claim_export_xml_row ">
<xsl:if test="claim_number =$mother_claim_no and current_onset_date != ''">
这样做的原因是元素的字符串值是其中所有文本的串联,因此该表达式将仅选择 current_onset_date
存在且包含非空字符串的行.如果你想排除只包含空格的元素,你可以这样写:
The reason this works is that the string value of an element is the concatenation of all the text inside it, so this expression will only select rows where current_onset_date
exists and contains a non-empty string. If you want to exclude elements that contain nothing but whitespace, you can write:
<xsl:for-each select="claim_export_xml_row ">
<xsl:if test="claim_number =$mother_claim_no and normalize-space( current_onset_date ) != ''">
这篇关于使用 XSLT 检查节点是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!