Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] XSLT to convert to type substitution

From: Farrukh Najmi <farrukh@--------------------->
To:
Date: 12/3/2007 1:29:00 AM
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent