xslt - Add additional xmlns:xsi attributes to XML element -


i have following xml defined:

<?xml version="1.0" encoding="utf-8"?> <ns0:container xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:ns0="ns0.com" xsi:schemalocation="ns0.com ns0.xsd">    <ns1:elementa xmlns:ns1="ns1.com" xsi:schemalocation="ns1.com ns1.xsd"/>    <ns2:elementb xmlns:ns2="ns2.com" xsi:schemalocation="ns2.com ns2.xsd"/> </ns0:container> 

the problem consuming application takes elements inside of container (unfortunately string cutting of container), , definition of namespace xsi missing.

i add xmlns:xsi="http://www.w3.org/2001/xmlschema-instance to each sub element of container too - reduntant specification, should not cause problems.

so result get:

<?xml version="1.0" encoding="utf-8"?> <ns0:container xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:ns0="container.com" xsi:schemalocation="ns0.com ns0.xsd">    <ns1:elementa xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:ns1="ns1.com" xsi:schemalocation="ns1.com ns1.xsd" />    <ns2:elementb xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:ns2="ns2.com" xsi:schemalocation="ns2.com ns2.xsd"/> </ns0:container> 

here xslt, tried several options, not able it:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xmlns:xsl="http://www.w3.org/1999/xsl/transform"      xmlns:ns0="ns0.com"     xmlns:ns1="ns1.com"     xmlns:ns2="ns2.com">     <xsl:output method="xml" indent="no"/>      <xsl:template match="ns0:container/*">         <xsl:copy>             <!-- here want add xmlns:xsi attribute -->             <xsl:attribute name="xsi">http://www.w3.org/2001/xmlschema-instance</xsl:attribute>             <!-- not work - how should that? -->             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template>      <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template>   </xsl:stylesheet> 

how can add additional xmlns:xsi="" element xslt?

try this

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:ns0="ns0.com">     <xsl:output method="xml" indent="no"/>     <xsl:template match="ns0:container/*">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template>     <xsl:template match="/*">         <xsl:element name="{name()}">             <xsl:apply-templates select="@*|node()"/>         </xsl:element>     </xsl:template>     <xsl:template match="/*/@xsi:*">         <xsl:attribute name="other:{local-name()}" namespace="{namespace-uri()}">             <xsl:value-of select="."/>         </xsl:attribute>     </xsl:template>     <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template> </xsl:stylesheet> 

edited:

you can simplify this

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="xml" indent="no"/>     <xsl:template match="/*">         <xsl:element name="{name()}" namespace="{namespace-uri()}">             <xsl:apply-templates select="@*|node()"/>         </xsl:element>     </xsl:template>     <xsl:template match="/*/@xsi:*">         <xsl:attribute name="other:{local-name()}" namespace="{namespace-uri()}">             <xsl:value-of select="."/>         </xsl:attribute>     </xsl:template>     <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template> </xsl:stylesheet> 

i think in xml have typo in declaration ns0's namespace (xmlns:ns0="container.com"). think mean (xmlns:ns0="ns0.com")


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -