Subject: [xsl] QName as attribute value: save and clean up namespace declarations From: "Heiko Niemann" <kontakt@xxxxxxxxxxxxxxxx> Date: Mon, 18 Jun 2012 13:39:31 +0200 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] How to do this tricky eli, Jo Na | Thread | Re: [xsl] QName as attribute value:, G. Ken Holman |
Re: [xsl] How to do this tricky eli, Michael Kay | Date | Re: [xsl] QName as attribute value:, G. Ken Holman |
Month |