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</mktValSO D>
<qtyIndaySOD>200</qtyInday SOD>
<mktValIndaySOD>20000</mkt ValIndaySO D>
</position>
<position>
<secID> 2</secID>
<qtySOD>200</qtySOD>
<mktValSOD>20000</mktValSO D>
<qtyIndaySOD>200</qtyInday SOD>
<mktValIndaySOD>30000</mkt ValIndaySO D>
</position>
<Account>
<envelope>
Output after Transform
-------------------------- ---------- ---------- ---------- ---------- ---------- --------
<envelope>
<account id="12">
<position>
<secID> 1</secID>
<qtySOD>100</qtySOD>
<mktValSOD>10000</mktValSO D>
<qtyIndaySOD>200</qtyInday SOD>
<mktValIndaySOD>20000</mkt ValIndaySO D>
</position>
<position>
<secID> 2</secID>
<qtySOD>200</qtySOD>
<mktValSOD>20000</mktValSO D>
<qtyIndaySOD>200</qtyInday SOD>
<mktValIndaySOD>30000</mkt ValIndaySO D>
</position>
<Account>
<envelope>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -
Thanks,
Sundeep
Here is the XML
---------------------
<envelope>
<account id="12">
<managed> 2 </managed>
<ppID> 23 </ppID>
<position>
<secID> 1</secID>
<qtySOD>100</qtySOD>
<mktValSOD>10000</mktValSO
<qtyIndaySOD>200</qtyInday
<mktValIndaySOD>20000</mkt
</position>
<position>
<secID> 2</secID>
<qtySOD>200</qtySOD>
<mktValSOD>20000</mktValSO
<qtyIndaySOD>200</qtyInday
<mktValIndaySOD>30000</mkt
</position>
<Account>
<envelope>
Output after Transform
--------------------------
<envelope>
<account id="12">
<position>
<secID> 1</secID>
<qtySOD>100</qtySOD>
<mktValSOD>10000</mktValSO
<qtyIndaySOD>200</qtyInday
<mktValIndaySOD>20000</mkt
</position>
<position>
<secID> 2</secID>
<qtySOD>200</qtySOD>
<mktValSOD>20000</mktValSO
<qtyIndaySOD>200</qtyInday
<mktValIndaySOD>30000</mkt
</position>
<Account>
<envelope>
--------------------------
Thanks,
Sundeep
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)
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>
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Gertone,
It is working fine. Quick questions: How to add an attribute to a node using XSLT?
Example
-------------------------- ---------- ------
<envelope ack="detail">
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>
<xsl:text>detail</xsl:text
</xsl:attribute>
ASKER