嵌套for循环,其中Xquery中的where条件

时间:2016-02-09 10:51:34

标签: xpath xquery exist-db

我需要从以下示例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()
)

2 个答案:

答案 0 :(得分:1)

问题是,您的XQuery中的$prod引用ClinicalDocument,这对您的目的来说不够具体。您希望在component内循环sectionstructuredBody,而不是从以下开始:

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

答案 1 :(得分:0)

使用单个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()