Solved

Include only few nodes in XML using XSLT

Posted on 2008-10-27
6
303 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:sundeepgopal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22819152
I already have code to delete the node. I want  XSLT code  to include the rest of the  nodes.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22819610
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

0
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22821157
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
 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 450 total points
ID: 22821218
Then you would get something like this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
<xsl:template match="envelope | account | position | secID | qtySOD | mktValSOD | qtyIndaySOD | mktValIndaySOD">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>
    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="*">
                <xsl:apply-templates select="node()"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22822029
Thanks Gertone,
It is working fine. Quick questions:  How to add an attribute to a node using XSLT?
Example
------------------------------------------
  <envelope ack="detail">

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22822060
<xsl:attribute name="ack">
   <xsl:text>detail</xsl:text>
</xsl:attribute>
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Post xml via PHP Soap Client 3 73
write xml in vb.net 2 41
xml with php question 5 36
Unexpected results on PowerShell ForEach loop over XML child nodes 5 24
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question