[Chicago-talk] Navigating an XSD

Jay Strauss me at heyjay.com
Fri Jan 23 08:56:37 PST 2015


Hi, now I'm a bit lost (having a hard time understanding the docs).  how do
I navigate to the element I want? (usable code below)

(I'm happy to do it with XML::Twig too if its easy)


I want to iterate through the "attributeGroup"s, getting their names, then
pull out each value for "attribute name" and for "fixed".

Thanks
Jay

#!/usr/bin/env perl

use strict;
use warnings;

use 5.10.0;
use Mojo::DOM;

use Data::Dumper;

my $xsd;
{
        local $/ = undef;
        $xsd=<DATA>;
}

my $dom = Mojo::DOM->new($xsd);


my @things = $dom->find('attributeGroup');


__DATA__
<?xml version ="1.0" encoding="UTF-8"?>
<!--
    v2.xml Message Definitions Version v2.5.1  - fields
    HL7® Version 2.5.1, © Health Level Seven, Inc.  All rights reserved.
HL7 and Health Level Seven are registered trademarks of Health Level Seven,
Inc.
-->
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:hl7-org:v2xml"
    xmlns:hl7="urn:com.sun:encoder-hl7-1.0"
    targetNamespace="urn:hl7-org:v2xml" xmlns:jaxb="
http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0">

    <!-- include datatypes definitions for version v2.5.1 -->
    <xsd:include schemaLocation="datatypes.xsd"/>

    <xsd:annotation>
        <xsd:appinfo source="urn:com.sun:encoder">
            <encoding xmlns="urn:com.sun:encoder" name="HL7 v2 Encoding"
namespace="urn:com.sun:encoder-hl7-1.0" style="hl7encoder-1.0"/>
        </xsd:appinfo>
    </xsd:annotation>

    <!--
        FIELD ABS.1
    -->
    <xsd:attributeGroup name="ABS.1.ATTRIBUTES">
        <xsd:attribute name="Item" type="xsd:string" fixed="1514"/>
        <xsd:attribute name="Type" type="xsd:string" fixed="XCN"/>
        <xsd:attribute name="Table" type="xsd:string" fixed="HL70010"/>
        <xsd:attribute name="LongName" type="xsd:string" fixed="Discharge
Care Provider"/>
    </xsd:attributeGroup>
    <xsd:complexType name="ABS.1.CONTENT">
        <xsd:annotation>
            <xsd:documentation xml:lang="en">Discharge Care
Provider</xsd:documentation>
            <xsd:appinfo source="urn:com.sun:encoder">
                <hl7:Item>1514</hl7:Item>
                <hl7:Type>XCN</hl7:Type>
                <hl7:Table>HL70010</hl7:Table>
                <hl7:LongName>Discharge Care Provider</hl7:LongName>
            </xsd:appinfo>
        </xsd:annotation>
        <xsd:complexContent>
            <xsd:extension base="XCN">
                <xsd:attributeGroup ref="ABS.1.ATTRIBUTES"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:element name="ABS.1" type="ABS.1.CONTENT"/>
    <!--
        FIELD ABS.2
    -->
    <xsd:attributeGroup name="ABS.2.ATTRIBUTES">
        <xsd:attribute name="Item" type="xsd:string" fixed="1515"/>
        <xsd:attribute name="Type" type="xsd:string" fixed="CE"/>
        <xsd:attribute name="Table" type="xsd:string" fixed="HL70069"/>
        <xsd:attribute name="LongName" type="xsd:string" fixed="Transfer
Medical Service Code"/>
    </xsd:attributeGroup>
    <xsd:complexType name="ABS.2.CONTENT">
        <xsd:annotation>
            <xsd:documentation xml:lang="en">Transfer Medical Service
Code</xsd:documentation>
            <xsd:appinfo source="urn:com.sun:encoder">
                <hl7:Item>1515</hl7:Item>
                <hl7:Type>CE</hl7:Type>
                <hl7:Table>HL70069</hl7:Table>
                <hl7:LongName>Transfer Medical Service Code</hl7:LongName>
            </xsd:appinfo>
        </xsd:annotation>
        <xsd:complexContent>
            <xsd:extension base="CE">
                <xsd:attributeGroup ref="ABS.2.ATTRIBUTES"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:element name="ABS.2" type="ABS.2.CONTENT"/>
</xsd:schema>



On Fri, Jan 23, 2015 at 6:56 AM, Joel Berger <joel.a.berger at gmail.com>
wrote:

> I am personal partial to Mojo::DOM though it does not have any actual XSD
> handling, it does xml just fine. That said, I would read through Matt
> Trout's recommendations here:
> http://shadow.cat/blog/matt-s-trout/mstpan-3/
>
> Joel
> On Jan 22, 2015 11:13 PM, "Jay Strauss" <me at heyjay.com> wrote:
>
>> Hi,
>>
>> I have an XSD document like below.  I've been googling and cpan-ing, but
>> can't find what I need (i know there are lots of XML packages), and I don't
>> know how to parse it using regexs (reliably).  I just want to read in this
>> XSD and navigate it like a perl structure, and extract field values like:
>>
>> Field:ABS.1
>> Item:1514
>> Type:XCN
>> Table:HL70010
>> LongName:Discharge Care Provider
>>
>> Field:ABS.2
>> ...
>>
>>
>> Can anyone recommend a module or a method?
>>
>> Thanks
>> Jay
>>
>>
>> <?xml version ="1.0" encoding="UTF-8"?>
>> <!--
>>     v2.xml Message Definitions Version v2.5.1  - fields
>>     HL7® Version 2.5.1, © Health Level Seven, Inc.  All rights
>> reserved.  HL7 and Health Level Seven are registered trademarks of Health
>> Level Seven, Inc.
>> -->
>> <xsd:schema
>>     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>>     xmlns="urn:hl7-org:v2xml"
>>     xmlns:hl7="urn:com.sun:encoder-hl7-1.0"
>>     targetNamespace="urn:hl7-org:v2xml" xmlns:jaxb="
>> http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0">
>>
>>     <!-- include datatypes definitions for version v2.5.1 -->
>>     <xsd:include schemaLocation="datatypes.xsd"/>
>>
>>     <xsd:annotation>
>>         <xsd:appinfo source="urn:com.sun:encoder">
>>             <encoding xmlns="urn:com.sun:encoder" name="HL7 v2 Encoding"
>> namespace="urn:com.sun:encoder-hl7-1.0" style="hl7encoder-1.0"/>
>>         </xsd:appinfo>
>>     </xsd:annotation>
>>
>>     <!--
>>         FIELD ABS.1
>>     -->
>>     <xsd:attributeGroup name="ABS.1.ATTRIBUTES">
>>         <xsd:attribute name="Item" type="xsd:string" fixed="1514"/>
>>         <xsd:attribute name="Type" type="xsd:string" fixed="XCN"/>
>>         <xsd:attribute name="Table" type="xsd:string" fixed="HL70010"/>
>>         <xsd:attribute name="LongName" type="xsd:string" fixed="Discharge
>> Care Provider"/>
>>     </xsd:attributeGroup>
>>     <xsd:complexType name="ABS.1.CONTENT">
>>         <xsd:annotation>
>>             <xsd:documentation xml:lang="en">Discharge Care
>> Provider</xsd:documentation>
>>             <xsd:appinfo source="urn:com.sun:encoder">
>>                 <hl7:Item>1514</hl7:Item>
>>                 <hl7:Type>XCN</hl7:Type>
>>                 <hl7:Table>HL70010</hl7:Table>
>>                 <hl7:LongName>Discharge Care Provider</hl7:LongName>
>>             </xsd:appinfo>
>>         </xsd:annotation>
>>         <xsd:complexContent>
>>             <xsd:extension base="XCN">
>>                 <xsd:attributeGroup ref="ABS.1.ATTRIBUTES"/>
>>             </xsd:extension>
>>         </xsd:complexContent>
>>     </xsd:complexType>
>>     <xsd:element name="ABS.1" type="ABS.1.CONTENT"/>
>>     <!--
>>         FIELD ABS.2
>>     -->
>>     <xsd:attributeGroup name="ABS.2.ATTRIBUTES">
>>         <xsd:attribute name="Item" type="xsd:string" fixed="1515"/>
>>         <xsd:attribute name="Type" type="xsd:string" fixed="CE"/>
>>         <xsd:attribute name="Table" type="xsd:string" fixed="HL70069"/>
>>         <xsd:attribute name="LongName" type="xsd:string" fixed="Transfer
>> Medical Service Code"/>
>>     </xsd:attributeGroup>
>>     <xsd:complexType name="ABS.2.CONTENT">
>>         <xsd:annotation>
>>             <xsd:documentation xml:lang="en">Transfer Medical Service
>> Code</xsd:documentation>
>>             <xsd:appinfo source="urn:com.sun:encoder">
>>                 <hl7:Item>1515</hl7:Item>
>>                 <hl7:Type>CE</hl7:Type>
>>                 <hl7:Table>HL70069</hl7:Table>
>>                 <hl7:LongName>Transfer Medical Service Code</hl7:LongName>
>>             </xsd:appinfo>
>>         </xsd:annotation>
>>         <xsd:complexContent>
>>             <xsd:extension base="CE">
>>                 <xsd:attributeGroup ref="ABS.2.ATTRIBUTES"/>
>>             </xsd:extension>
>>         </xsd:complexContent>
>>     </xsd:complexType>
>>     <xsd:element name="ABS.2" type="ABS.2.CONTENT"/>
>>
>> _______________________________________________
>> Chicago-talk mailing list
>> Chicago-talk at pm.org
>> http://mail.pm.org/mailman/listinfo/chicago-talk
>>
>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20150123/6be30dd7/attachment.html>


More information about the Chicago-talk mailing list