Ashok.N
Ashok.N

Reputation: 1391

exclude-result-prefixes in XSLT is not working

I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<comments xmlns="http://www.dsttechnologies.com/awd/rest/v1">
   <row xmlns="">
      <source>SYSTEM</source>
      <group>PRTL-TASK</group>
      <type>text/plain</type>
      <text mimetype="text/plain">Assigned to: DT73606</text>
      <date>2015-08-18</date>
      <posted>8 days ago</posted>
      <time>04:04:58-05:00</time>
      <userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
         <lastName xmlns="http://www.dsttechnologies.com/awd/rest/v1">All powerful UserId</lastName>
      </userNameInfo>
      <instanceId>2015-07-29-04.27.15.461040T01</instanceId>
      <link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/b5ee129a-0338-41c7-881b-ab8c1727de36" />
      <link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
   </row>
   <row xmlns="">
      <source>MANUAL</source>
      <group>PRTL-TASK</group>
      <type>text/plain</type>
      <text mimetype="text/plain">hai hello</text>
      <date>2015-07-29</date>
      <posted>3 weeks ago</posted>
      <time>09:04:25-05:00</time>
      <userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
         <lastName xmlns="http://www.dsttechnologies.com/awd/rest/v1">All powerful UserId</lastName>
      </userNameInfo>
      <instanceId>2015-07-29-04.27.15.461040T01</instanceId>
      <link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/6c09f55e-f06f-4fa1-abe4-4bc4eed14e6d" />
      <link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
   </row>
</comments>

I am trying to modify the above xml using XSLT in such a way that the output is as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<History>
   <comments>
      <row>
         <source>SYSTEM</source>
         <group>PRTL-TASK</group>
         <type>text/plain</type>
         <text mimetype="text/plain">Assigned to: DT73606</text>
         <date>2015-08-18</date>
         <posted>8 days ago</posted>
         <time>04:04:58-05:00</time>
         <userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
            <lastName>All powerful UserId</lastName>
         </userNameInfo>
         <instanceId>2015-07-29-04.27.15.461040T01</instanceId>
         <link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/b5ee129a-0338-41c7-881b-ab8c1727de36" />
         <link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
      </row>
      <row>
         <source>MANUAL</source>
         <group>PRTL-TASK</group>
         <type>text/plain</type>
         <text mimetype="text/plain">hai hello</text>
         <date>2015-07-29</date>
         <posted>3 weeks ago</posted>
         <time>09:04:25-05:00</time>
         <userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
            <lastName>All powerful UserId</lastName>
         </userNameInfo>
         <instanceId>2015-07-29-04.27.15.461040T01</instanceId>
         <link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/6c09f55e-f06f-4fa1-abe4-4bc4eed14e6d" />
         <link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
      </row>
   </comments>
</History>

The ultimate goal is to eliminate the namespaces. I am trying with the following XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="http://www.dsttechnologies.com/awd/rest/v1" version="2.0" exclude-result-prefixes="v">
   <xsl:strip-space elements="*" />
   <xsl:output indent="yes" omit-xml-declaration="yes" />
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="v:comments">
      <History>
         <xsl:copy>
            <xsl:apply-templates select="v:comment" />
         </xsl:copy>
      </History>
   </xsl:template>
   <xsl:template match="v:comment">
      <row>
         <xsl:apply-templates mode="sans-namespace" />
      </row>
   </xsl:template>
   <xsl:template match="*" mode="sans-namespace">
      <xsl:element name="{local-name()}">
         <xsl:apply-templates select="@*|node()" />
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

I am not getting the desired output despite using exclude-result-prefixes="v".Could anybody please point out what's wrong here?

Upvotes: 1

Views: 3477

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117140

When you copy an element, you copy its namespace too. This has nothing to do with exclude-result-prefixes: namespaces that are in actual use will never be excluded.

In your example, you could do simply:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v="http://www.dsttechnologies.com/awd/rest/v1"
exclude-result-prefixes="v">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes"  encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/v:comments">
    <History>
        <comments>
            <xsl:apply-templates/>
        </comments>
    </History>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

potame
potame

Reputation: 7905

Changing the template:

<xsl:template match="v:comments">
   <History>
      <xsl:copy>
         <xsl:apply-templates select="v:comment" />
      </xsl:copy>
   </History>
</xsl:template>

to

<xsl:template match="v:comments">
   <History>
      <comments>
         <xsl:apply-templates select="@*|node()" />
      </comments>
   </History>
</xsl:template>

should work in your case.

However, XSLT 2.0 provide the copy-namespaces and inherit-namespaces on the <xsl:copy> instruction but they don't seem to be working.

Upvotes: 0

Related Questions