xsl-list
[Top] [All Lists]

Re: [xsl] XSLT to convert to type substitution

2007-12-02 18:28:42
Thank you so very much Florent for helping me out :-)
I am trying it out now.

In my case there is a pattern between the original element name and the type name, where elmenet name of xx:Foo will always map to type name of xx:FooType. I will try my limited XSL skills to see if I can figure out how to adapt you XSLT so I need not have a mapping file and instead use the pattern above.

Thanks again. This community rock!

Florent Georges wrote:
Farrukh Najmi wrote:

  Hi

I have an XML Schema that is being changed to use type
substitution instead of substitutionGroups (element
substitution).

I was wondering if any one can share an XSLT sample that
would convert according to examples of old and new
document below...

  Provided you have a mapping document like the following:

    <mapping xmlns:xx="xx-uri">
       <xx:Element1 xx:Element1="type" xx:Object="elem"/>
       <xx:Element2 xx:Element2="type" xx:Object="elem"/>
    </mapping>

you can use the Modified Identity pattern as following.  Take
care to correctly handle the namespace bindings.

  Regards,

--drkm


Welcome to the Emacs shell

~/xslt/tests $ cat farrukh.xml <xx:Objects xmlns:xx="xx-uri">
   <xx:Element1>
      ...
   </xx:Element1>
   <xx:Element2>
      ...
   </xx:Element2>
</xx:Objects>

~/xslt/tests $ cat farrukh-mapping.xml <mapping xmlns:xx="xx-uri">
   <xx:Element1 xx:Element1="type" xx:Object="elem"/>
   <xx:Element2 xx:Element2="type" xx:Object="elem"/>
</mapping>

~/xslt/tests $ cat farrukh.xsl <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xs="http://www.w3.org/2001/XMLSchema";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                xmlns:my="my:farrukh.xsl"
                exclude-result-prefixes="xs my"
                version="2.0">

   <xsl:variable name="mapping" as="element()+" select="
       document('farrukh-mapping.xml')/mapping/*"/>

   <xsl:function name="my:name-mapping" as="element()?">
      <xsl:param name="elem" as="element()"/>
      <xsl:sequence select="
          $mapping[node-name(.) eq node-name($elem)]"/>
   </xsl:function>

   <xsl:template match="node()">
      <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="/*">
      <xsl:copy>
         <xsl:namespace name="xsi" select="
             'http://www.w3.org/2001/XMLSchema-instance'"/>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="*[my:name-mapping(.)]">
      <xsl:variable name="m" as="element()" select="
          my:name-mapping(.)"/>
      <xsl:variable name="e" as="xs:QName" select="
          $m/@*[. eq 'elem']/node-name(.)"/>
      <xsl:variable name="t" as="xs:QName" select="
          $m/@*[. eq 'type']/node-name(.)"/>
      <xsl:element name="{ $e }"
                   namespace="{ namespace-uri-from-QName($e) }">
         <xsl:namespace name="{ prefix-from-QName($t) }"
                        select="namespace-uri-from-QName($t)"/>
         <xsl:attribute name="xsi:type">
            <xsl:value-of select="$t"/>
         </xsl:attribute>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="node()"/>
      </xsl:element>
   </xsl:template>

</xsl:stylesheet>

~/xslt/tests $ saxon farrukh.xml farrukh.xsl
<?xml version="1.0" encoding="UTF-8"?><xx:Objects xmlns:xx="xx-uri"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
   <xx:Object xsi:type="xx:Element1">
      ...
   </xx:Object>
   <xx:Object xsi:type="xx:Element2">
      ...
   </xx:Object>
</xx:Objects>



--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>