[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

QName as attribute value: save and clean up namespace

Subject: QName as attribute value: save and clean up namespace declarations
From: "Heiko Niemann" <kontakt@xxxxxxxxxxxxxxxx>
Date: Mon, 18 Jun 2012 13:39:31 +0200
 QName as attribute value: save and clean up namespace
Hello,

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.

Thanks,
Heiko

---------
SOURCE
---------

<?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>



----------------
STYLESHEET
----------------

<?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:if test="namespace-uri-for-prefix($var.prefix,.)">
        <xsl:namespace name="{$var.prefix}"
select="namespace-uri-for-prefix($var.prefix,.)"/>
      </xsl:if>
      <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>



-------------
RESULT
-------------

<?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">
    <v:name>Basic</v:name>
    <v:number>A200338</v:number>
    <v:color>blue</v:color>
  </v:item>

  <v:item xsi:type="ns2:advancedItem" xmlns:ns2="http://prods.com/ns2">
    <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>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.