xslt 1.0 - Transform which produces default namespace convention xmlns="..." -


given following xml:

<root xmlns="example.com">   <child /> </root> 

what xslt (version 1.0) can used produce:

<newroot xmlns="stackoverflow.com">   <child /> </newroot> 

i've tried various combinations of exclude-result-prefixes , namespace-alias. e.g,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:e="example.com" xmlns:s="stackoverflow.com" exclude-result-prefixes="e">  <xsl:namespace-alias stylesheet-prefix="s" result-prefix="#default" />      <xsl:template match="e:root">         <s:newroot>             <xsl:apply-templates />         </s:newroot>     </xsl:template>      <xsl:template match="e:child">         <s:child />     </xsl:template>  </xsl:stylesheet> 

the closest i've come following incorrect xml

<newroot>   <child /> </newroot> 

edit: apologize overly simple sample. solution i'm searching change default namespace , make transformations xml. here more sophisticated example of do:

<data xmlns="sample.com/public">   <insured>     <name>sample client</name>     <locations>       <location number="1">         <address>           <city>new york</city>           <state>new york</state>           <street>222 10 street</street>         </address>       </location>       <location number="2">         <address>           <city>new york</city>           <state>new york</state>           <street>3538 27 street</street>         </address>       </location>     </locations>     <coverages>       <locationsplit>         <locationnumber>1</locationnumber>         <clause>           <limit>10000000</limit>         </clause>       </locationsplit>     </coverages>   </insured> </data> 

which become

<projection xmlns="sample.com/projected">   <insured>     <name>sample client</name>     <location address="222 10 street new york, new york">       <clause>         <limit>10000000</limit>       </clause>     </location>   </insured> </projection> 

i thought exclude-result-prefix , namespace-alias ticket because explicitly use each namespace declaring them in stylesheet , remove /public namespace while aliasing /projection namespace #default. however, alias causes namespace removed.

it difficult understand in question real , example. take example literally (as own answer does), answer be:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns="stackoverflow.com" xmlns:e="example.com"  exclude-result-prefixes="e"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:template match="/e:root">     <newroot>         <xsl:apply-templates />     </newroot> </xsl:template>  <xsl:template match="e:child">     <child/> </xsl:template>  </xsl:stylesheet> 

a more generic answer, move all elements existing namespace/s new namespace be:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:template match="*">     <xsl:element name="{local-name()}" namespace="stackoverflow.com">         <xsl:apply-templates/>     </xsl:element> </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -