Re: [xsl] XSLT to convert to type substitution

Subject: Re: [xsl] XSLT to convert to type substitution
From: Farrukh Najmi <farrukh@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 02 Dec 2007 20:27:56 -0500
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:


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

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"/>

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



Welcome to the Emacs shell

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

~/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"/>

~/xslt/tests $ cat farrukh.xsl <xsl:stylesheet xmlns:xsl="";
exclude-result-prefixes="xs my"

   <xsl:variable name="mapping" as="element()+" select="

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

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

   <xsl:template match="*[my:name-mapping(.)]">
      <xsl:variable name="m" as="element()" select="
      <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) }"
         <xsl:attribute name="xsi:type">
            <xsl:value-of select="$t"/>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="node()"/>


~/xslt/tests $ saxon farrukh.xml farrukh.xsl
<?xml version="1.0" encoding="UTF-8"?><xx:Objects xmlns:xx="xx-uri"
   <xx:Object xsi:type="xx:Element1">
   <xx:Object xsi:type="xx:Element2">

Farrukh Najmi


Current Thread