在 Xquery 中使用 where 条件嵌套 for 循环
nested for loop with where condition in Xquery
我需要从以下示例中过滤标签值 XML。
<ClinicalDocument xmlns="urn:hl7-org:v3">
<id root="3930E379-5C54-477D-8DB2-F6C92BC08C691" />
<component>
<structuredBody>
<component>
<section>
<templateId root="1.3.6.1.4.1.19376.1.5.3.1.3.4"/>
<code code="10164-2" codeSystem="2.16.840.1.113883.6.1"
codeSystemName="LOINC" displayName="HISTORY OF PRESENT ILLNESS"/>
<title>HISTORY OF PRESENT ILLNESS</title>
<text>Patient slipped and fell on ice, twisting her ankle as she fell.
</text>
</section>
</component>
<component>
<section>
<templateId root="1.3.6.1.4.1.19376.1.5.3.1.3.5"/>
<code code="10164-3" codeSystem="2.16.840.1.113883.6.12"
codeSystemName="LOINC1" displayName="DEMO"/>
<title>DEMO HISTORY OF PRESENT ILLNESS</title>
<text>DEMO Patient slipped and fell on ice, twisting her ankle as she fell.
</text>
</section>
</component>
</structuredBody>
</component>
</ClinicalDocument>
我的 collection 中有很多这样的文件(我正在使用 eXits-db),我需要根据 <id>
标签中的 'root' 属性进行过滤,并且'root' 属性在 <templateId>
标签中。而我需要的结果只是 <title>
文本值。
以下是查询 i tried.But 显示了所有标题值(不是符合我的条件的那个)。
xquery version "3.0";
declare namespace d = "urn:hl7-org:v3";
(
for $prod in collection("/db/netspectivedb/")/d:ClinicalDocument
where $prod/d:id/@root/string()='3930E379-5C54-477D-8DB2-F6C92BC08C691'
and $prod/d:component/d:structuredBody/d:component/d:section/d:templateId/@root/string()='1.3.6.1.4.1.19376.1.5.3.1.3.4'
return $prod/d:component/d:structuredBody/d:component/d:section/d:title/text()
)
问题是,您的 XQuery 中的 $prod
引用了 ClinicalDocument
,这对于您的目的而言不够具体。您想在 structuredBody
中遍历 component
或 section
而不是开始,例如 :
declare namespace d = "urn:hl7-org:v3";
(
for $section in collection("/db/netspectivedb/")/d:ClinicalDocument[d:id/@root eq '3930E379-5C54-477D-8DB2-F6C92BC08C691']/d:component/d:structuredBody/d:component/d:section
where $section/d:templateId/@root eq '1.3.6.1.4.1.19376.1.5.3.1.3.4'
return $section/d:title/text()
)
或根据您的具体要求使用嵌套 for
。在这种情况下,嵌套 for
也变得更具可读性:
declare namespace d = "urn:hl7-org:v3";
(
for $prod in collection("/db/netspectivedb/")/d:ClinicalDocument
for $section in $prod/d:component/d:structuredBody/d:component/d:section
where $prod/d:id/@root eq '3930E379-5C54-477D-8DB2-F6C92BC08C691'
and $section/d:templateId/@root eq '1.3.6.1.4.1.19376.1.5.3.1.3.4'
return $section/d:title/text()
)
我使用 eq
而不是上面的 =
,因为我们打算进行 值比较 (阅读更多:https://developer.marklogic.com/blog/comparison-operators-whats-the-difference)
您可以使用单个 XPath 表达式实现相同的目的:
declare namespace d = "urn:hl7-org:v3";
collection("/db/netspectivedb/")/
d:ClinicalDocument[d:id/@root eq '3930E379-5C54-477D-8DB2-F6C92BC08C691']/
d:component/d:structuredBody/d:component/
d:section[d:templateId/@root eq '1.3.6.1.4.1.19376.1.5.3.1.3.4']/d:title/text()
我需要从以下示例中过滤标签值 XML。
<ClinicalDocument xmlns="urn:hl7-org:v3">
<id root="3930E379-5C54-477D-8DB2-F6C92BC08C691" />
<component>
<structuredBody>
<component>
<section>
<templateId root="1.3.6.1.4.1.19376.1.5.3.1.3.4"/>
<code code="10164-2" codeSystem="2.16.840.1.113883.6.1"
codeSystemName="LOINC" displayName="HISTORY OF PRESENT ILLNESS"/>
<title>HISTORY OF PRESENT ILLNESS</title>
<text>Patient slipped and fell on ice, twisting her ankle as she fell.
</text>
</section>
</component>
<component>
<section>
<templateId root="1.3.6.1.4.1.19376.1.5.3.1.3.5"/>
<code code="10164-3" codeSystem="2.16.840.1.113883.6.12"
codeSystemName="LOINC1" displayName="DEMO"/>
<title>DEMO HISTORY OF PRESENT ILLNESS</title>
<text>DEMO Patient slipped and fell on ice, twisting her ankle as she fell.
</text>
</section>
</component>
</structuredBody>
</component>
</ClinicalDocument>
我的 collection 中有很多这样的文件(我正在使用 eXits-db),我需要根据 <id>
标签中的 'root' 属性进行过滤,并且'root' 属性在 <templateId>
标签中。而我需要的结果只是 <title>
文本值。
以下是查询 i tried.But 显示了所有标题值(不是符合我的条件的那个)。
xquery version "3.0";
declare namespace d = "urn:hl7-org:v3";
(
for $prod in collection("/db/netspectivedb/")/d:ClinicalDocument
where $prod/d:id/@root/string()='3930E379-5C54-477D-8DB2-F6C92BC08C691'
and $prod/d:component/d:structuredBody/d:component/d:section/d:templateId/@root/string()='1.3.6.1.4.1.19376.1.5.3.1.3.4'
return $prod/d:component/d:structuredBody/d:component/d:section/d:title/text()
)
问题是,您的 XQuery 中的 $prod
引用了 ClinicalDocument
,这对于您的目的而言不够具体。您想在 structuredBody
中遍历 component
或 section
而不是开始,例如 :
declare namespace d = "urn:hl7-org:v3";
(
for $section in collection("/db/netspectivedb/")/d:ClinicalDocument[d:id/@root eq '3930E379-5C54-477D-8DB2-F6C92BC08C691']/d:component/d:structuredBody/d:component/d:section
where $section/d:templateId/@root eq '1.3.6.1.4.1.19376.1.5.3.1.3.4'
return $section/d:title/text()
)
或根据您的具体要求使用嵌套 for
。在这种情况下,嵌套 for
也变得更具可读性:
declare namespace d = "urn:hl7-org:v3";
(
for $prod in collection("/db/netspectivedb/")/d:ClinicalDocument
for $section in $prod/d:component/d:structuredBody/d:component/d:section
where $prod/d:id/@root eq '3930E379-5C54-477D-8DB2-F6C92BC08C691'
and $section/d:templateId/@root eq '1.3.6.1.4.1.19376.1.5.3.1.3.4'
return $section/d:title/text()
)
我使用 eq
而不是上面的 =
,因为我们打算进行 值比较 (阅读更多:https://developer.marklogic.com/blog/comparison-operators-whats-the-difference)
您可以使用单个 XPath 表达式实现相同的目的:
declare namespace d = "urn:hl7-org:v3";
collection("/db/netspectivedb/")/
d:ClinicalDocument[d:id/@root eq '3930E379-5C54-477D-8DB2-F6C92BC08C691']/
d:component/d:structuredBody/d:component/
d:section[d:templateId/@root eq '1.3.6.1.4.1.19376.1.5.3.1.3.4']/d:title/text()