Question

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?

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top