24

I have the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
    <Columns>
        <Column Description="FirstName" MaxRange="1" MinRange="0" Name="FirstName" SQLDataType="12" SourceColumn="FirstName"/>
        <Column Description="LastName" MaxRange="1" MinRange="0" Name="LastName" SQLDataType="12" SourceColumn="LastName"/>
        <Column Description="Phone" MaxRange="1" MinRange="0" Name="Phone" SQLDataType="1" SourceColumn="Phone"/>
    </Columns>
    <Row>
        <FirstName>Michael</FirstName>
        <LastName>David</LastName>
        <Phone>1234567890</Phone>
    </Row>
    <Row>
        <FirstName>David</FirstName>
        <LastName>Michael</LastName>
        <Phone>01234567890</Phone>
    </Row>
    <Row>
        <FirstName>Yang</FirstName>
        <LastName>Christina</LastName>
        <Phone>2345678901</Phone>
    </Row>
    <Row>
        <FirstName>Grey</FirstName>
        <LastName>Meredith</LastName>
        <Phone>3456789012</Phone>
    </Row>
    <Row>
        <FirstName>David</FirstName>
        <LastName>Shepherd</LastName>
        <Phone>5678901234</Phone>
    </Row>
</Rowset>

I want to remove <Phone> node from every Row as well as from Column description.

SO my resultant XML would look like following:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
    <Columns>
        <Column Description="FirstName" MaxRange="1" MinRange="0" Name="FirstName" SQLDataType="12" SourceColumn="FirstName"/>
        <Column Description="LastName" MaxRange="1" MinRange="0" Name="LastName" SQLDataType="12" SourceColumn="LastName"/>
    </Columns>
    <Row>
        <FirstName>Michael</FirstName>
        <LastName>David</LastName>
    </Row>
    <Row>
        <FirstName>David</FirstName>
        <LastName>Michael</LastName>
    </Row>
    <Row>
        <FirstName>Yang</FirstName>
        <LastName>Christina</LastName>
    </Row>
    <Row>
        <FirstName>Grey</FirstName>
        <LastName>Meredith</LastName>
    </Row>
    <Row>
        <FirstName>David</FirstName>
        <LastName>Shepherd</LastName>
    </Row>
</Rowset>

How do I achieve that? I tried various XSLT but I am not able to do it.

1 Answer 1

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

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

<xsl:template match="Column[@SourceColumn='Phone']|Phone" />

</xsl:stylesheet>
5
  • 1
    Hi Sean. Thanks for the answer. It does suffice my requirements. But I did not understand how does it achive that? Can you please explain the XSLT?
    – Soham Shah
    Aug 28, 2012 at 17:36
  • 16
    The first template copies everything as is from the input document, except for the nodes that match the second template. The second template matches elements with name Phone or elements with name Column and attribute SourceColumn=Phone. For these nodes, do nothing. That is to say, delete them. Aug 28, 2012 at 22:55
  • 3
    "except for the nodes that match the second template" where does it do that exception?
    – Blub
    Feb 6, 2015 at 10:49
  • 8
    @Blub: When the conditions are that both templates match, the template with the most specific match criteria prevails. So for example, when the element is 'Phone', both templates match, but the second template is more specific, so it wins. Feb 9, 2015 at 0:54
  • 1
    Also, keep in mind, while this particular example XML does not have namespaces, you will have to include them in your XPath if you have XML with namespaces. Jun 7, 2019 at 10:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.