Reputation: 335
I want xml transformation using xslt. Here is the xml that needs to be transformed:
This fails because there is missing namespace for the attribute value "xsi:type" which is "Insert", I want it as "ns:Insert". I tried using online xsl transformer(http://www.utilities-online.info/xsltransformation/#.WrtA4S5uZEQ), it works fine there, but once I put it in my code. I do not get the desired prefix. Any reason for that ??
Upvotes: 1
Views: 103
Reputation: 30991
I had to add xmlns:v1="http://stil.dk/ipung/services/synclokationer/v1.0"
to your source XML, otherwise it was not well-formed.
I used the below script (slightly modified your version):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1="http://stil.dk/ipung/services/synclokationer/v1.0"
xmlns:ns="http://www.logica.com/veu/syncSkole/dto/Lokationer"
exclude-result-prefixes="v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:com="http://www.logica.com/veu/syncSkole/dto/common">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{local-name()}"><xsl:apply-templates select="@* | node()"/></xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
<xsl:template match="comment() | text() | processing-instruction()">
<xsl:copy />
</xsl:template>
<xsl:template match="v1:Lokation">
<xsl:element name="{local-name()}">
<xsl:attribute name="xsi:type">
<xsl:value-of select="translate(@xsi:type, 'v1', 'ns')" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:element>
</xsl:template>
<xsl:template match="v1:Modtager | v1:ModtagerSystemID |
v1:ModtagerSystemTransaktionsID | v1:Afsendelsestidspunkt |
v1:BeskedID | v1:InstNr">
<xsl:element name="{local-name()}"
namespace="http://www.logica.com/veu/syncSkole/dto/common">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<!-- Caution: Namespace different than above -->
<xsl:template match="v1:syncLokationer">
<xsl:element name="ns:{local-name()}"
namespace="http://www.logica.com/veu/syncSkole/dto/Lokationer">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Under www.utilities-online.info/xsltransformation I got Lokation
element as follows:
<Lokation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="ns:Insert">
So xsi:type
attribute has value ns:Insert
, just as you expected.
As Parfait proposed, I modified the script to eliminate repeating templates, differing only in match attribute.
Upvotes: 2