XML Tutorials

  Home arrow XML Tutorials arrow Page 4 - Joins and Query Use with XML
XML TUTORIALS

Joins and Query Use with XML
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-11

    Table of Contents:
  • Joins and Query Use with XML
  • 9.6 Implementing the W3C XML Query-Use Cases in XSLT
  • Use case TREE: queries that preserve hierarchy
  • Use case SEQ: queries based on sequence.

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Joins and Query Use with XML - Use case SEQ: queries based on sequence.


    (Page 4 of 4 )

    This use case illustrates queries based on the sequence in which elements appear in a document. Although sequence is not significant in most traditional database systems or object systems, it can be important in structured documents. This use case presents a series of queries based on a medical report:

      <!DOCTYPE report[
        <!ELEMENT report (section*)>
        <!ELEMENT section (section.title, section.content)>
        <!ELEMENT section.title (#PCDATA )>
        <!ELEMENT section.content (#PCDATA | anesthesia | prep
                      | incision | action | observation )*>
        <!ELEMENT anesthesia (#PCDATA)>
        <!ELEMENT prep ( (#PCDATA | action)* )>
        <!ELEMENT incision ( (#PCDATA | geography | instrument)* )>
        <!ELEMENT action ( (#PCDATA | instrument )* )>
        <!ELEMENT observation (#PCDATA)>
        <!ELEMENT geography (#PCDATA)>
        <!ELEMENT instrument (#PCDATA)>
    ]>
    <report>
      <section> 
        <section.title>Procedure</section. title>
         <section.content>
          The patient was taken to the operating room where she was placed
          in supine position and
          <anesthesia>induced under general anesthesia.</anesthesia>
          <prep>
            <action>A Foley catheter was placed to decompress the bladder</action>
            and the abdomen was then prepped and draped in sterile fashion.
          </prep>
          <incision>
            A curvilinear incision was made
            <geography>in the midline immediately infraumbilical</geography>
            and the subcutaneous tissue was divided
            <instrument>using electrocautery.</instrument>
          </incision>
          The fascia was identified and
          <action>#2 0 Maxon stay sutures were placed on each side of the midline.
          </action>
          <incision>
            The fascia was divided using 
            <instrument>electrocautery </instrument>
            and the peritoneum was entered.
          </incision>
          <observation>The small bowel was identified.</observation>
          and
          <action>
            the
            <instrument>Hasson trocar</instrument>
            was placed under direct visualization.
          </action>
          <action>
            The
            <instrument>trocar</instrument>
            was secured to the fascia using the stay sutures.
          </action>
         </section.content>
      </section>
    </report>
    Question 1. In the Procedure section of Report1, what instruments were used in the second incision?
    <xsl:template match="section[section.title = 'Procedure']"> <xsl:copy-of select="(.//incision)[2]/instrument"/>
    </xsl:template>
    Question 2. In the Procedure section of Report1, what are the first two instruments to be used?
    <xsl:template match="section[section.title = 'Procedure']"> <xsl:copy-of select="(.//instrument)[position() &lt;= 2]"/>
    </xsl:template>
    Question 3. In Report1, what instruments were used in the first two actions after
    the second incision?
    <xsl:template match="report">
    <!-- i2 = Second incision in the entire report -->
    <xsl:variable name="i2" select="(.//incision)[2]"/>
    <!-- Of all the actions following i2
         get the instruments used in the first two -->
    <xsl:copy-of
         select="($i2/following::action)[position() &lt;= 2]/instrument"/>
    </xsl:template>
    Question 4. In Report1, find "Procedure" sections for which no anesthesia element
    occurs before the first incision: <xsl:template match="section[section.title = 'Procedure']">
      <xsl:variable name="i1" select="(.//incision)[1]"/>
      <xsl:if test=".//anesthesia[preceding::incision = $i1]">
        <xsl:copy-of select="current()"/>
      </xsl:if>
    </xsl:template>
    If the result is not empty, then a major lawsuit is soon to follow! Question 5. In Report1, what happened between the first and second incision? <xsl:template match="report"> <critical_sequence>
      <!-- i1 = First incision in the entire report -->
      <xsl:variable name="i1" select="(.//incision)[1]"/>
      <!-- i2 = Second incision in the entire report -->
      <xsl:variable name="i2" select="(.//incision)[2]"/>
      <!-- copy all sibling nodes following i1
        that don't have a preceding element i2 and are not themeseves i2 -->
      <xsl:for-each select="$i1/following-sibling::node()
                 [not(./preceding::incision = $i2) and not(. = $i2)]">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </critical_sequence>
    </xsl:template>

    In Questions 4 and 5, I assume that the string values of incision elements are unique. This is true in the sample data, but may not be true in the most general case. To be precise, you should apply Recipe 4.2. For example, in Question 4, the test should be:

      test=".//anesthesia[count(./preceding::incision | $i1) =
      count(./preceding::incision)]"

    Please check back  next week for the conclusion to this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    XML TUTORIALS ARTICLES

    - Validation with Document Type Definitions (D...
    - Creating a Well-Formed XML Document
    - Getting to Know XML
    - A Friendly Approach to XML
    - Creating RSS 2.0 Feeds
    - Using Modules in Your RSS Feed
    - RSS 2.0
    - Querying XML: Use Cases
    - Joins and Query Use with XML
    - Solving Problems by Querying XML
    - Performing Set Operations When Querying XML
    - Querying XML
    - Handling Data for Ajax with JSON
    - Handling XML Data for Ajax
    - XML and JSON for Ajax


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap