首先,我想问一下,XML节点的以下两种说法有区别吗:
- 检查节点是否为空节点;
- 检查节点是否存在;
假设我有一个这样的 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 是否不包含任何内容”。
谁能告诉我我的错误在哪里,并帮助我解决开头列出的问题,谢谢!
最佳答案
I would like to ask, is there a difference between the following two statements for XML nodes:
1.check whether a node is an empty node;
2.check whether a node exists or not;
这些需要不同的表达式来测试:不存在的节点不是空节点:
current_onset_date
这会选择当前节点的任何 current_onset_date
子节点。当且仅当至少存在一个这样的 child 时,它的 bool 值为 true()
,否则为 false()
— 否则。
current_onset_date/text()
这会选择当前节点的任何 current_onset_date
子节点的任何文本节点子节点。如果没有,则其 bool 值为false()
,否则--true()
,
即使一个元素没有文本节点作为子元素,它仍然可能有一个非空字符串值,因为它可能有元素作为后代元素,并且其中一些元素后代元素可能有文本节点子元素。
current_onset_date[not(string(.))]
这会选择当前节点的所有 current_onset_date
子节点,这些子节点的字符串值为空字符串 ('')。这可能很适合“空元素”。
如果您所说的空是指字符串值为空或仅空白的元素,则此表达式:
current_onset_date[not(normalize-space())]
这会选择当前节点的所有 current_onset_date
子节点,这些子节点的字符串值为空字符串 ('') 或纯空格字符串。
Could anybody tell me where my mistake is
在您的代码中:
<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()]
关于xml - 使用 XSLT 检查节点是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4694480/