[英]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>
我的收藏夾中有很多這樣的文件(我正在使用eXits-db),我需要根據<id>
標簽中的'root'屬性和<templateId>
標簽中的'root'屬性進行過濾。 而我需要的結果只是<title>
文本值。
以下是我嘗試過的查詢,但是顯示了所有標題值(不是符合我條件的標題值)。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.