0

I have the following XML

<CATALOG>
  <PLANT>Bloodroot</PLANT>
  <PLANT>Columbine</PLANT>
  <PLANT>Marsh Marigold</PLANT>
  <PLANT>Cowslip</PLANT>
</CATALOG>

and try to filter the first two but including the parent CATALOG using XPATH

<CATALOG>
  <PLANT>Bloodroot</PLANT>
  <PLANT>Columbine</PLANT>
</CATALOG>

This

 /CATALOG/PLANT[position() <=2]

extract the plants without the catalog, but how do I say: add the enclosing CATALOG?

Dieter Menne
  • 10,076
  • 44
  • 67

1 Answers1

0

Check: XPATH: select subset of xml file

You should write an xsl to avoid this problem like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="PLANT[position() <=2]" />

    <!--identity template to copy all nodes and attributes to output -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
Community
  • 1
  • 1
Gyorgy.Hegedus
  • 361
  • 1
  • 3