Solved

Include only few nodes in XML using XSLT

Posted on 2008-10-27
6
296 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing information on Duplicate XML Nodes 24 78
PHP MySQL MySQLi Update Insert, and mapping XML data to MySQL Columns 13 80
Adjust the position 3 60
.php tree directory? 5 73
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

777 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