Ask the Expert

Modifying XML rows using XSL sheets

Can you explain how to select and modify or delete a row in an XML file using XSL sheets?
The way you do it is by having an XSLT that selectively copies to the target output. Here's one such file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/ | @* | node()">
    <xsl:if test="not(local-name(.)='Customer' and @Id='02')"> 
      <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Look at the <xsl:if> element, that tests that the current node is not a Customer with an Id attribute of "2", and in that case, it performs the copy.

For a source file like the following:

<?xml version="1.0" encoding="utf-8" ?> 
<Root>
  <Customer Id="01" />
  <Customer Id="02" />
</Root>
You'd get the following output:
<?xml version="1.0" encoding="utf-8" ?> 
<Root>
  <Customer Id="01" />
</Root>

This was first published in October 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: