Wojciech
Wojciech

Reputation: 63

How to use XSLT to replace an element that is in a namespace?

I need to replace an element in an XML with a new element using XSL Transformation. The input XML contains a namespace declaration. I did manage to match the element by declaring the same namespace in XSLT and adding the namespace prefix in the template. But I cannot insert the new element in the same namespace.

Input XML

<?xml version="1.0" encoding="utf-8"?>
<ledesxmlebilling2.1 xmlns="http://www.ledes.org/ledes21.xsd">
  <firm>
    <client>
      <invoice>
        <matter>
          <tksum>
            <tk_id>Sample ID</tk_id>
            <tk_lname>Sample last name</tk_lname>
            <tk_fname />
            <tk_level>Sample level</tk_level>
            <tk_rate>Sample rate</tk_rate>
          </tksum>
        </matter>
      </invoice>
    </client>
  </firm>
</ledesxmlebilling2.1>

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ledes="http://www.ledes.org/ledes21.xsd" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

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

  <!-- tksum template -->
  <xsl:template match="ledes:tksum">
    <tksum>
      <tk_id>New ID</tk_id>
      <tk_lname>New last name</tk_lname>
      <tk_fname />
      <tk_level>New level</tk_level>
      <tk_rate>New rate</tk_rate>
    </tksum>
  </xsl:template>
</xsl:stylesheet>

Current output XML

<?xml version="1.0" encoding="utf-8"?>
<ledesxmlebilling2.1 xmlns="http://www.ledes.org/ledes21.xsd">
  <firm>
    <client>
      <invoice>
        <matter>
          <tksum xmlns="" xmlns:ledes="http://www.ledes.org/ledes21.xsd">
            <tk_id>New ID</tk_id>
            <tk_lname>New last name</tk_lname>
            <tk_fname />
            <tk_level>New level</tk_level>
            <tk_rate>New rate</tk_rate>
          </tksum>
        </matter>
      </invoice>
    </client>
  </firm>
</ledesxmlebilling2.1>

Desired output XML

<?xml version="1.0" encoding="utf-8"?>
<ledesxmlebilling2.1 xmlns="http://www.ledes.org/ledes21.xsd">
  <firm>
    <client>
      <invoice>
        <matter>
          <tksum>
            <tk_id>New ID</tk_id>
            <tk_lname>New last name</tk_lname>
            <tk_fname />
            <tk_level>New level</tk_level>
            <tk_rate>New rate</tk_rate>
          </tksum>
        </matter>
      </invoice>
    </client>
  </firm>
</ledesxmlebilling2.1>

How should I change the XSLT to get the desired output XML?

Upvotes: 1

Views: 78

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117043

I cannot insert the new element in the same namespace.

It's actually quite simple - try:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ledes="http://www.ledes.org/ledes21.xsd" 
exclude-result-prefixes="ledes">
<xsl:output method="xml" version="1.0" 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="ledes:tksum">
    <tksum xmlns="http://www.ledes.org/ledes21.xsd">
        <tk_id>New ID</tk_id>
        <tk_lname>New last name</tk_lname>
        <tk_fname />
        <tk_level>New level</tk_level>
        <tk_rate>New rate</tk_rate>
    </tksum>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions