Link to home
Start Free TrialLog in
Avatar of sundeepgopal
sundeepgopal

asked on

Include only few nodes in XML using XSLT

Please help me with XSL.  I'm new to XSL.

Here is the XML
---------------------
<envelope>
   <account id="12">
   <managed> 2 </managed>
   <ppID> 23 </ppID>
   
  <position>
    <secID> 1</secID>
    <qtySOD>100</qtySOD>
    <mktValSOD>10000</mktValSOD>
    <qtyIndaySOD>200</qtyIndaySOD>
    <mktValIndaySOD>20000</mktValIndaySOD>
   
  </position>

  <position>
    <secID> 2</secID>
    <qtySOD>200</qtySOD>
    <mktValSOD>20000</mktValSOD>
    <qtyIndaySOD>200</qtyIndaySOD>
    <mktValIndaySOD>30000</mktValIndaySOD>

  </position>
<Account>  

<envelope>


Output after Transform
------------------------------------------------------------------------------------
<envelope>
   <account id="12">

  <position>
    <secID> 1</secID>
    <qtySOD>100</qtySOD>
    <mktValSOD>10000</mktValSOD>
    <qtyIndaySOD>200</qtyIndaySOD>
    <mktValIndaySOD>20000</mktValIndaySOD>
   
  </position>

  <position>
    <secID> 2</secID>
    <qtySOD>200</qtySOD>
    <mktValSOD>20000</mktValSOD>
    <qtyIndaySOD>200</qtyIndaySOD>
    <mktValIndaySOD>30000</mktValIndaySOD>

  </position>
<Account>  
<envelope>
---------------------------------------------------------------------------------------

Thanks,
Sundeep
Avatar of sundeepgopal
sundeepgopal

ASKER

I already have code to delete the node. I want  XSLT code  to include the rest of the  nodes.
Avatar of Gertone (Geert Bormans)
Here is an XSLT with two templates
First template copies all
The second is a specialisation, that removes two nodes
(a moire specialised template match takes precedence, so this two nodes are not copied)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node()">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>
 <xsl:template match="managed | ppID"/>
</xsl:stylesheet>

Open in new window

Hi Gertone,

I want it the other way. I might add some more nodes to xml in the future. So It would be great if you tell me to include the nodes that needed rather than removing the unwanted nodes.

Thanks,
Sundeep
 
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Gertone,
It is working fine. Quick questions:  How to add an attribute to a node using XSLT?
Example
------------------------------------------
  <envelope ack="detail">

<xsl:attribute name="ack">
   <xsl:text>detail</xsl:text>
</xsl:attribute>