Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a very big XML document getting processed by XSLT. Other than adding security token in header, I am passing the message as it is to backend by using apply-templates. I am not even parsing the message. But now, I have to rename one node element present in the body of message.

Can anyone tell me how can we achieve that without spoiling apply-templates?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
284 views
Welcome To Ask or Share your Answers For Others

1 Answer

As you didn't provide an example input, just as suggestion following XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
 <xsl:strip-space elements="*"/>
  <xsl:template match="/">
     <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="rename">
    <new>
      <xsl:apply-templates select="@*|node()"/>
    </new>
  </xsl:template>
</xsl:stylesheet>

when applied to this example input XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <keep>Text</keep>
 <keep>Text</keep>
 <keep>Text</keep>
 <rename>Text</rename>
</root>

produces the output

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <keep>Text</keep>
  <keep>Text</keep>
  <keep>Text</keep>
  <new>Text</new>
</root>

So based on the node that you want to rename you can just match it with a separate template

<xsl:template match="rename">

write the new name for the element and apply templates to write the content

<new>
  <xsl:apply-templates select="@*|node()"/>
</new>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...