1

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?

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

or use xsl:copy instead of redefining an element with the same name and bound to the same namespace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.