2

I have an xml file of following form

         <input>
            <node1>No Update<node1>
            <node2>No Update<node2>
            <node3>No Update<node3>
            <node4>Data<node4>
         <input>

My requirement is to update the values of those nodes which has data 'No Update' into empty string, so that the desired output will be

         <input>
            <node1><node1>
            <node2><node2>
            <node3><node3>
            <node4>Data<node4>
         <input> 

How can I achieve it using xslt, can somebody help please.

1
  • Thanks it helps, i have got one more doubt here. The above input xml is the output of some xslt transformations. If I add the above code to my existing xsl at the last, will it remove the values? These <input> xml node is copied from another xml based on some conditions. Can we remove the values while copying? Commented May 25, 2015 at 5:48

2 Answers 2

2

Use this XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="*[text() = 'No Update']/text()"/>

</xsl:stylesheet>

And if you don't want self-enclosing tags in output change the template to:

<xsl:template match="*[text() = 'No Update']/text()">
  <xsl:value-of select="''"/>
</xsl:template>
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer, +1. Which would also be same with xsl:value-of <xsl:template match="*[text() = 'No Update']/text()"/>.
0

Just use this short(er) transformation -- the same idea of overriding the identity rule, but matching only the necessary text-nodes -- not element nodes.:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="text()[. = 'No Update']"/>
</xsl:stylesheet>

When this transformation is applied on the provided XML (corrected to become a well-formed XML document):

<input>
    <node1>No Update</node1>
    <node2>No Update</node2>
    <node3>No Update</node3>
    <node4>Data</node4>
</input>

The wanted, correct result is produced:

<input>
   <node1/>
   <node2/>
   <node3/>
   <node4>Data</node4>
</input>

Comments

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.