0

I need to transform an XML document which does not use any schema to another format which uses a well defined schema.

So basically I have to transform this:

<healthCareFacilityTypeCode 
     displayName="Home" 
     codingScheme="Connect-a-thon healthcareFacilityTypeCodes"
    >Home</healthCareFacilityTypeCode>

Into this:

<healthCareFacilityTypeCode>
    <code>Home</code>
    <displayName>
        <LocalizedString value="Home" />
    </displayName>
    <schemeName>Connect-a-thon healthcareFacilityTypeCodes</schemeName>
</healthCareFacilityTypeCode>

I know how to transform it by hand by looking at the schema. Here is a snippet of the XSD:

<xsd:complexType name="DocumentEntryType">
    <xsd:sequence>
        <xsd:element minOccurs="0" 
                     name="healthCareFacilityTypeCode" 
                     type="tns:CodedMetadataType"/>
    </xsd:sequence>
    <xsd:attribute default="false" 
                   name="existing" 
                   type="xsd:boolean" 
                   use="optional"/>
</xsd:complexType>
<xsd:element name="DocumentEntry" type="tns:DocumentEntryType"/>

What I don't know how to tackle is: how to exploit the target XSD to transform a node from the source XML to the target XML doc. I feel all the info to perform the transform is located in the XSD but can I use it? how?

Any help will be greatly appreciated!

SamB
  • 9,039
  • 5
  • 49
  • 56
code-gijoe
  • 6,949
  • 14
  • 67
  • 103
  • 4
    The target XSD will help you to validate the result of the transformation, but won't directly help produce the transformation. However a good XSLT IDE, like Oxygen and probably StylusStudio, will help you validate your output elements to a large extent *as you develop the stylesheet*. Also a schema-aware XSLT 2.0 processor can validate its output at runtime, given the schema for the result document. – LarsH Apr 19 '11 at 20:10
  • @LarsH XSLT IDEs could go one step further and use the XSD to provide auto-completion for literal-result-elements/attributes where they are permitted in the XSLT. The main issue with this is that, unless all elements and attributes are declared at the top-level of the XSD (globally), there's a potential for ambiguity if there's insufficient context. Also, this can't help where xsl:element and xsl:attribute instructions are used instead of the literal-result equivalents, but I think its better than nothing. – pgfearo Apr 20 '11 at 03:35
  • @pgfearo - I think IDEs do provide assisted completion as you described, but I don't used XSD much so I'm not sure. As you say it's still not a complete solution, but can be helpful. – LarsH Apr 20 '11 at 16:47
  • 1
    @LarsH Well, some provide a list of all possible elements, but they don't refine this list according to context, or highlight literal result elements with sequence constructors for content that is known (with static context) to be invalid - i.e. there's room for improvement – pgfearo Apr 20 '11 at 21:03

1 Answers1

1

Followed suggestions and this is what I came up with. Not perfect but it is enough for my purpose.

    <xsl:template match="XDSDocumentEntry">
        <DocumentEntryType>
            <xsl:call-template name="namespaceChange"/>
            <xsl:apply-templates/>
        </DocumentEntryType>
    </xsl:template>
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//*[matches(name(), 'Code')]">
        <xsl:copy>
            <code>
                <xsl:value-of select="."/>
            </code>
            <schemeName>
                <xsl:value-of select="@codingScheme"/>
            </schemeName>
            <displayName>
                <LocalizedString>
                    <xsl:attribute name="value">
                        <xsl:value-of select="@displayName"/>
                    </xsl:attribute>
                </LocalizedString>
            </displayName>
        </xsl:copy>
    </xsl:template>
code-gijoe
  • 6,949
  • 14
  • 67
  • 103
  • 3
    You can't produce namespace declarations using xsl:attribute - namespaces and attributes are not the same thing in the XSLT data model. A conformant XSLT processor will throw this out. In XSLT 2.0, use xsl:namespace. – Michael Kay Apr 20 '11 at 21:07
  • Thank you. I made the changes. – code-gijoe Apr 20 '11 at 23:44
  • But this doesn't answer your question because you didn't ask for an XSLT stylesheet... –  Apr 21 '11 at 02:12