Re: [xsl] QName as attribute value: save and clean up namespace declarations

Subject: Re: [xsl] QName as attribute value: save and clean up namespace declarations
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 18 Jun 2012 07:50:28 -0400
At 2012-06-18 13:39 +0200, Heiko Niemann wrote:
in this example I have an order with different typed items. The type is a
QName in an attribute. There are namespaces declared, having the same
prefix as the 'prefix' in the xsi:type attribute. As you can see there are
more namespaces declared as needed (whatever the reason for that case
might be). Now I would like to clean up and just keep the ones that are
'used' in the xsi:type content. The stylesheet actually already does what
I want, but I would like to know whether there might be a better way to do
it, since it looks a little hackish to me.

The only "improvement" I can think of is to simply copy the namespace node rather than reconstitute it.


  <!--identity tranformation; do not copy namespaces; create namespace of
xsi:type-->
  <xsl:template match="*[@xsi:type]">
    <xsl:variable name="var.prefix"
select="substring-before(@xsi:type,':')"/>
    <xsl:copy copy-namespaces="no">
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select=
            "namespace::*[name(.)=substring-before(current()/@xsi:type,':')]"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

But, really, I don't see why you would need to change what you have.

. . . . . . . . . . Ken

~/t/ftemp $ cat heiko.xml
<?xml version="1.0" encoding="UTF-8"?>
<c:order xmlns:c="http://prods.com/c";
         xmlns:v="http://prods.com/v";
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>

  <v:item xsi:type="ns1:basicItem"
          xmlns:ns1="http://prods.com/ns1";
          xmlns:ns2="http://prods.com/ns2";
          xmlns:ns3="http://prods.com/ns3";>
    <v:name>Basic</v:name>
    <v:number>A200338</v:number>
    <v:color>blue</v:color>
  </v:item>

  <v:item xsi:type="ns2:advancedItem"
          xmlns:ns1="http://prods.com/ns1";
          xmlns:ns2="http://prods.com/ns2";
          xmlns:ns3="http://prods.com/ns3";>
    <v:name>Advanced</v:name>
    <v:number>A200339</v:number>
    <v:color>white</v:color>
    <v:pattern>stripes</v:pattern>
  </v:item>

  <v:item xsi:type="ns4:lostItem"
          xmlns:ns1="http://prods.com/ns1";
          xmlns:ns2="http://prods.com/ns2";
          xmlns:ns3="http://prods.com/ns3";>
    <v:name>Lost</v:name>
    <v:number>A200340</v:number>
  </v:item>

</c:order>~/t/ftemp $ xslt2 heiko.xml heiko.xsl
<?xml version="1.0" encoding="UTF-8"?><c:order xmlns:c="http://prods.com/c"; xmlns:v="http://prods.com/v"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>


  <v:item xmlns:ns1="http://prods.com/ns1"; xsi:type="ns1:basicItem">
    <v:name>Basic</v:name>
    <v:number>A200338</v:number>
    <v:color>blue</v:color>
  </v:item>

  <v:item xmlns:ns2="http://prods.com/ns2"; xsi:type="ns2:advancedItem">
    <v:name>Advanced</v:name>
    <v:number>A200339</v:number>
    <v:color>white</v:color>
    <v:pattern>stripes</v:pattern>
  </v:item>

  <v:item xsi:type="ns4:lostItem">
    <v:name>Lost</v:name>
    <v:number>A200340</v:number>
  </v:item>

</c:order>~/t/ftemp $ cat heiko.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
version="2.0">
  <xsl:output method="xml" encoding="UTF-8"/>

  <!--identity transformation; copy namespaces-->
  <xsl:template match="*">
    <xsl:copy copy-namespaces="yes">
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!--identity tranformation; do not copy namespaces; create namespace of
xsi:type-->
  <xsl:template match="*[@xsi:type]">
    <xsl:variable name="var.prefix"
select="substring-before(@xsi:type,':')"/>
    <xsl:copy copy-namespaces="no">
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select=
            "namespace::*[name(.)=substring-before(current()/@xsi:type,':')]"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!--identity transformation; do not copy namespaces-->
  <xsl:template match="*[@xsi:type]//*">
    <xsl:copy copy-namespaces="no">
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>~/t/ftemp $


-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012 Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread