Processing XML/CDA Documents Paul V. Biron, MLIS Kaiser Permanente, So Cal Medical Group Pasadena, CA Co-Chair HL7 XML SIG Co-Chair HL7 Structured Document TC HL7 Fall Plenary Meeting Sept 14, 2000 XSL(T), DOM and SAX
ProcessingXML/CDA Documents
Paul V. Biron, MLISKaiser Permanente, So Cal Medical Group
Pasadena, CA
Co-Chair HL7 XML SIGCo-Chair HL7 Structured Document TC
HL7 Fall Plenary MeetingSept 14, 2000
XSL(T), DOM and SAX
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
2
Intro
• Extensible Stylesheet Language (XSL)– XSL Transformation Language (XSLT)
– XML Path Language (XPath)
– CDA Transformation Issues
• Document Object Model (DOM)
• Simple API for XML (SAX)
• Namespaces in XML [optional]
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
4
XSL
• Alphabet Soup– XSL - Exstensible Stylesheet Language– XSLT - XSL Transformation Language– XPath - XML Path Language
• XSL can be used to:– Generate XHTML
– Generate other XML
– Generate the XSL formatting language as ahierarchical sequence of formatting objects
• images, text lines, paragraph blocks ,etc.
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
5
XSL: Generate XHTML
XSLT transformation
XSLstylesheet
XMLdoc-
ument
XHTMLdoc-
ument
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
6
XSL: XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match=’pattern’>{action}
</xsl:template>
<xsl:template match=’pattern’>{action}
</xsl:template></xsl:stylesheet>
XSL Document
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
7
XSL: Formatting Objects (FOs)
• Pagination and Layout Formatting Objects
• Block Formatting Objects
• Inline Formatting Objects
• Formatting Objects for Tables
• Formatting Objects for Lists
• Link and Multi Formatting Objects
• Out-of-Line Formatting Objects
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
8
XSL: Generate Formatting Objects
XSLT transformation
XSL formattingengine
Consists of the tags andattributes which representspecifications for the XSLformating engine
Output to display,printer, Postscript, etc.
XSLstylesheet
XMLdoc-
ument
XSLFOs
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
9
Radiology Report
Patient Information• Name: Henry Levin, the 7th• MRN: 123456789• SEX: Male• DOB: May 13, 1923
Procedure : Chest X-RayDate: November 07, 1999
Clinical DataHistory of smoking for 40 years.
FindingsRLL nodule.
ImpressionsRLL nodule, suggestive of malignancy.Compared with a prior CXR from 6 monthsago, nodule size has increased.
RecommendationsI notified the ordering physician of this findingby phone.
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN>123456789</MRN> <SEX>Male</SEX> <DOB>May 13, 1923</DOB> <PROCEDURE>Chest X-Ray <ID code="P5-20100" system="SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
Locally applied markup
Transformation Use-cases
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
10
<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> < SEX>Male</ SEX> < DOB>May 13, 1923</ DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
Locally applied markup CDA markup
Element Sequence
Note: CDA markup not currentper the membership ballot
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
11
Locally applied markup CDA markup
Element vs. Attribute<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX>Male</SEX> <DOB> May 13, 1923</DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
12
Locally applied markup
Required Element<levelone> <clinical_document_header> <id V="N3" AA="2.16.840.1.1138.933"/> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX> Male</SEX> <DOB> May 13, 1923</DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
13
Locally applied markup
Vocabulary Domains<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> < SEX>Male</ SEX> <DOB> May 13, 1923</DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
14
Locally applied markup
Different (lexical) Datatypes<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX> Male</SEX> < DOB>May 13, 1923</ DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
15
Locally applied markup
Different Definitions<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX> Male</SEX> <DOB> May 13, 1923</DOB> <PROCEDURE>Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
16
XSLT
• stylesheet is a sequence of templates– template can be thought of as rules
• each template has 2 parts– match pattern
• determines which element/attribute/etc. nodes thetemplate applies to
– template body• a combination of explicit markup and XSLT
commands
• determines the transformation that will beperformed on the nodes matched by pattern
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
17
XSLT: Processing Model
• Set the current node to the documentnode
• find patterns that match– choose the "best" pattern
– apply the template• typically selects one or more additional nodes to
process
– appending result to the output tree
• Recurse until no new nodes are selected
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
18
Match an element name
<xsl:template match="section">{action}
</xsl:template>
XPath: Simple Patterns
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
19
<xsl:template match="body|caption|content">{action}
</xsl:template>
XPath: Simple Patterns
Match any one of many elementnames
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
20
XPath: Simple Patterns
Match an attribute name
<xsl:template match="@ignore">{action}
</xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
21
XPath: Complex Patterns
Match a "path"
<xsl:template match="section/section/paragraph">{action}
</xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
22
XPath: Complex Patterns
Match a predicate
<xsl:template match="observation_media.value [@MD=’image/gif’ or @MD=’image/jpeg]">
{action}</xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
23
XPath: Misc PatternsMatch the context node
<xsl:template match=".">{action}
</xsl:template>
Match the parent node
<xsl:template match="..">{action}
</xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
24
XPath: Misc PatternsMatch any element
<xsl:template match="*">{action}
</xsl:template>
Match any attribute
<xsl:template match="@*">{action}
</xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
25
XPath: Misc Patterns
Match a node type
<xsl:template match="text()">{action}
</xsl:template>
<xsl:template match="comment()">{action}
</xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
26
XSLT: Built-In Templates
<xsl:template match="*|/"><xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()|@*"><xsl:value-of select="."/>
</xsl:template>
<xsl:template match="processing-instruction()|comment()"/></xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
27
XSLT: Commands (Actions)
<xsl:apply-templates/>
<xsl:apply-templates select=’pattern’/>
apply-templates
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
28
XSLT: Commands (Actions)
Conditionals: if
<xsl:if test=’predicate’>{action}
</xsl:if>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
29
XSLT: Commands (Actions)Conditionals: choose (if/then/else)
<xsl:choose><xsl:when test=’predicate’>
{action}<xsl:/when><xsl:when test=’predicate’>
{action}</xsl:when><xsl:otherwise>
{action}</xsl:otherwise>
</xsl:choose>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
30
XSLT: Commands (Actions)
Repetition: for-each
<xsl:for-each select=’pattern’>{action}
</xsl:for-each>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
31
XSLT: Commands (Actions)
Sorting
<xsl:for-each select=’pattern’><xsl:sort order=’ascending|descending’/>
</xsl:for-each>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
32
XSLT: Commands (Actions)
Getting an element’s content or anattribute’s value
<xsl:value-of select=’pattern’/>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
33
XSLT: Commands (Actions)
<xsl:variable name=’name’ select=’pattern’/>
<xsl:value-of select=’$name’/>
Setting a variable
Getting a variable’s value
<xsl:apply-templates select=’pattern-with-$variable’/>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
34
<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> < SEX>Male</ SEX> < DOB>May 13, 1923</ DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
Locally applied markup
Element SequenceCDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
35
<xsl:template match=’Header’> ... <xsl:apply-templates select=’DOB’/> <xsl:apply-templates select=’SEX’/> ...</xsl:template>
Element Sequence
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
36
Locally applied markup
Element vs. Attribute<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX>Male</SEX> <DOB> May 13, 1923</DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
37
<xsl:template match=’SEX’> <administrative_gender_cd> <xsl:attribute name=’V’> <xsl:choose> <xsl:when test=’text()="Male"’>M</xsl:when> <xsl:when test=’text()="Female"’>F</xsl:when> </xsl:choose> </xsl:attribute> </administrative_gender_cd></xsl:template>
Element vs. Attribute
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
38
Locally applied markup
Required Element<levelone> <clinical_document_header> <id V="a123" AA="2.16.840.1.1138.933"/> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX> Male</SEX> <DOB> May 13, 1923</DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
39
<xsl:template match=’Header’> ... <id AA=’2.16.840.1.1138.933’> <xsl:attribute name=’V’>
<xsl:value-of select=’generate-id()’/> </xsl:attribute> </id> ...</xsl:template>
Required Element
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
40
Locally applied markup
Vocabulary Domains<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> < SEX>Male</ SEX> <DOB> May 13, 1923</DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
41
<xsl:template match=’SEX’> <administrative_gender_cd> <xsl:attribute name=’V’> <xsl:choose> <xsl:when test=’text()="Male"’>M</xsl:when> <xsl:when test=’text()="Female"’>F</xsl:when> </xsl:choose> </xsl:attribute> </administrative_gender_cd></xsl:template>
Vocabulary Domains
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
42
Locally applied markup
Different (lexical) Datatypes<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX> Male</SEX> < DOB>May 13, 1923</ DOB> <PROCEDURE> Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
43
Different (lexical) Datatypes
<xsl:template match=’DOB’> <birth_dttm> <xsl:attribute name=’V’> <xsl:call-template name=’format-date’> <xsl:with-param name=’date’> <xsl:value-of select=’text()’/> </xsl:with-param> </xsl:call-template> </xsl:attribute> </birth_dttm></xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
44
Locally applied markup
Different Definitions<levelone> <clinical_document_header> <clinical_document_header.service_cd V="P5-20100" PN="Chest X-Ray" S="2.16.840.1.113883.6.1"/> <patient_encounter> <encounter_tmr LOW="19991107"/> </patient_encounter> <patient> <patient.type_cd V="PAT"/> <person> <id V="123456789" AA="2.16.840.113.933"/> <person_name> <nm><G V="Henry"/> <F V="Levin"/> <SF V="the 7th"/></nm> <person_name.type_cd V="L"/> </person_name> </person> <birth_dttm V="19320513"/> <administrative_gender_cd V="M"/> </patient> </clinical_document_header> <body> … </body></levelone>
<RadiologyReport> <Header> <Name> Henry Levin, the 7th </Name> <MRN> 123456789</MRN> <SEX> Male</SEX> <DOB> May 13, 1923</DOB> <PROCEDURE>Chest X-Ray <ID code=" P5-20100" system=" SNOMED"/> <DATE> November 07, 1999 </DATE> </PROCEDURE> </Header> <Body> … </Body></RadiologyReport>
CDA markup
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
45
Different Definitions<xsl:template match=’PROCEDURE’> <clinical_document_header.service_cd> <xsl:attribute name=’V’> <xsl:value-of select=’ID/@code’/> </xsl:attribute> <xsl:attribute name=’PN’> <xsl:value-of select=’text()’/> </xsl:attribute> <xsl:attribute name=’S’> <xsl:call-template name=’map-code-system’> <xsl:with-param name=’system’ select=’ID/@system’/> </xsl:call-template> </xsl:attribute> </clinical_document_header.service_cd></xsl:template>
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
47
What is the DOM?
• Document Object Model (DOM)– Tree-based API
• Represents a parsed document as tree of typednodes
– element, attribute, characterData, etc.
• Processing consists of traversing the tree andperforming operations on the nodes
• W3C Recommendation– Current version: Level 1
• Level 2 in "candidate recommendation" stage now
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
48
<?xml version="1.0"?><hello level="0">
<world>goodbye</world><hello level="1">
<world>goodbye</world></hello>
</hello>
Tree-based APIs
Level=0Level=0hellohello
worldworld hellohello
worldworld
Level=1Level=1
goodbyegoodbye
goodbyegoodbye
elementselements
attrsattrs
charDatacharData
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
49
Hello World, DOM-styleimport org.w3c.dom.* ; import org.xml.sax.* ;import com.ibm.xml.parsers.DOMParser ; import java.io.* ;public class HelloWorld{
public static void main (String[] args){
Document domTree = createDOMTree (args[0]) ;traverseDOMTree (domTree.getDocumentElement (), 0) ;
}static void traverseDOMTree (Node node, int indent){
for (int i = 0 ; i < indent ; i++) {System.out.print ("\t") ;}
System.out.println(node.getNodeName() + ":" + node.getNodeValue()) ;
if (node.hasChildNodes ()) {NodeList nl = node.getChildNodes () ;for (int i = 0 ; i < nl.getLength () ; i++) {
traverseDOMTree (nl.item (i), indent + 1) ;}
}}static Document createDOMTree (String xmlFile) {…}
}
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
50
Hello World, DOM-Style
c:\>type helloworld.xml<?xml version="1.0"?><hello>
<world>goodbye</world></hello>
c:\>java HelloWorld helloworld.xmlhello:null
#text:\n\tworld:null
#text:goodbye#text:\n
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
51
Node Types
• Document
• Element
• Attr
• Comment
• Text
• CDATASection
• DocumentType
• EntityReference
• Entity
• DocumentFragment
• Notation
• ProcessingInstruction
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
52
DOM: Fundamental Interfaces
• Attr
• CharacterData
• Document
• Element
• Node
• Text
• NodeList
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
53
MethodsMethodsNode Interface
• getAttributes
• getChildNodes
• getFirstChild
• getLastChild
• getNextSibling
• getOwnerDocument
• getNodeName
• getNodeType
• getNodeValue
• getParentNode
• getPreviousSibling
• hasChildNodes
• appendChild
• cloneNode
• insertBefore
• removeChild
• replaceChild
• setNodeValue
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
54
MethodsMethods
Element Interface
• getAttributeNode
• setAttributeNode
• removeAttributeNode
• normalize
• getTagName
• getAttribute
• setAttribute
• removeAttribute
• getElementsByTagName
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
55
MethodsMethods
Attr Interface
• getValue
• setValue
• getName
• getSpecified
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
56
Get an attribute
import org.w3c.dom.* ; import org.xml.sax.* ;import com.ibm.xml.parsers.DOMParser ; import java.io.* ;public class HelloWorld{
public static void main (String[] args){
Document domTree = createDOMTree (args[0]) ;String sex = getPatientSex (domTree.getDocumentElement ()) ;
System.out.println ("sex is " + sex) ;}public static String getPatientSex (Document doc){
NodeList nl = doc.getElementsByTagName("administrative_gender_cd") ;
Element sex = (Element) nl.item (0) ;Attr value = sex.getAttributeNode ("V") ;
return (value.getValue ()) ;}static Document createDOMTree (String xmlFile) {…}
}
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
58
What is SAX?
• Simple API for XML (SAX)– Event-based API
• Represent a parsed document as a sequence ofparse events
– startElement, characterData, endElement, etc.
• Processing consists of dispatching events from anevent queue or handling callbacks registered foreach event
• NOT a W3C Recommendation– a "de-facto" standard
– originally designed by David Megginson
– design continues on XML-DEV mailing list
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
59
Hello World, SAX-Style
import org.xml.sax.* ; import org.xml.sax.helpers.* ;import java.io.* ;public class HelloWorldSAXHelper extends HandlerBase{// this helper class should be instantiated by a static main() somewhere// the following methods implement the SAX DocumentHandler interface
public void startElement (String name, AttributeList atts){
System.out.println ("startElement: " + name) ;}public void characters (char c[], int x, int y){
System.out.println ("characters: " + new String (c, x, y)) ;}public void endElement (String name){
System.out.println ("endElement: " + name) ;}public void ignorableWhitespace (char c[], int x, int y){
System.out.println("ignorableWhitespace: "+ new String(c, x, y));}
}
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
60
Hello World, SAX-Stylec:\>type helloworld.xml<?xml version="1.0"?><hello>
<world>goodbye</world></hello>
c:\>java HelloWorldSAX helloworld.xmlstartElement: helloignorableWhitespace:\n\tstartElement: worldcharacters: goodbyeendElement: worldignorableWhitespace:\nendElement: hello
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
61
Application WritersApplication Writers Parser WritersParser Writers
Interfaces
• DocumentHandler
• ErrorHandler
• DTDHandler
• EntityResolver
• Parser
• AttributeList
• Locator
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
62
MethodsMethods
DocumentHandler Interface
• startDocument
• startElement
• characters
• endElement
• endDocument
• ignorableWhitespace
• setDocumentLocator
• processingInstruction
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
63
MethodsMethods
Parser Interface
• parse
• setDocumentHandler
• setDTDHandler
• setEntityResolver
• setErrorHandler
• setLocale
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
64
MethodsMethods
AttributeList Interface
• getLength
• getName
• getType
• getValue
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
65
Outro
• Extensible Stylesheet Language (XSL)– XSL Transformation Language (XSLT)
– XML Path Language (XPath)
• Document Object Model (DOM)
• Simple API for XML (SAX)
• Namespaces in XML [optional]
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
67
Namespaces in XML
• An XML namespace is a collection ofnames, identified by a URI reference,which are used in XML documents aselement types and attribute names
• Names from XML namespaces appear asqualified names, which contain a singlecolon, separating the name into anamespace prefix and a local part
• The prefix, which is mapped to a URIreference, selects a namespace.
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
68
Namespace Declarations
<!ELEMENT NAME (First, Middle?, Last, Suffix?)><!ELEMENT Name xmlns CDATA #IMPLIED>
<RadiologyReport> <PatientInfo> <Name xmlns="http://www.radiology.org/report"> <First>Amy</First> <Middle>A.</Middle> <Last>Fall</Last> </Name> … </PatientInfo> ….</RadiologyReport>
XML Document
Default NamespaceThe namespace name in the attribute value is that of the default namespace inthe scope of the element to which the declaration is attached. In such a defaultdeclaration, the attribute value may be empty.
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
69
Namespace Declarations
<!ELEMENT NAME (First, Middle?, Last, Suffix?)><!ELEMENT Name xmlns:radorg CDATA #IMPLIED>
<RadiologyReport> <PatientInfo> <Name xmlns:radorg="http://www.radiology.org/report"> <First>Amy</First> <Middle>A.</Middle> <Last>Fall</Last> </Name> … </PatientInfo> ….</RadiologyReport>
XML Document
Namespace PrefixThe local part gives the namespace prefix, used to associate element andattribute names with the namespace name in the attribute value in the scope ofthe element to which the declaration is attached.
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
70
Qualified NamesStart tag Content End tag
<<radorgradorg:Name> :Name> Female </</radorgradorg:Name>:Name>
Namespace qualified element name
Start tag Attribute Content End tag
<patient.sex hl7:table=<patient.sex hl7:table=“HL70001“HL70001”>> Female</ patient.sex></ patient.sex>
Namespace qualified attribute
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
71
Qualified Names
<!ELEMENT NAME (First, Middle?, Last, Suffix?)><!ELEMENT Name xmlns:radorg CDATA #IMPLIED>
<RadiologyReport> <PatientInfo> <radorg:Name xmlns:radorg="http://www.radiology.org/report"> <First>Amy</First> <Middle>A.</Middle> <Last>Fall</Last> </radorg:Name> … </PatientInfo> ….</RadiologyReport>
XML Document
Namespace PrefixThe local part gives the namespace prefix, used to associate element andattribute names with the namespace name in the attribute value in the scope ofthe element to which the declaration is attached.
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
72
References• XSL
– W3C. Extensible Stylesheet Language (XSL).W3C Working Draft.
– http://www.w3.org/TR/WD-xsl
• XPath– W3C. XML Path Language. W3C
Recommendation.– http://www.w3.org/TR/xpath
• XSLT– W3C. XSL Transformations (XSLT). W3C
Recommendation.– http://www.w3.org/TR/xslt
2000-05-22 Paul V. Biron, Processing XML/PRA Documents: DOM, SAX and XSL
73
References• DOM
– W3C. Document Object Model (DOM)Level 1 Specification. W3C Recommendation.
– http://www.w3.org/TR/REC-DOM-Level-1
• SAX– XML-DEV. Simple API for XML (SAX). De-
facto Standard.– http://www.megginson.com/SAX
• XML Names– W3C. Namespaces in XML. W3C
Recommendation.– http://www.w3.org/TR/REC-xml-names