Solved

Include only few nodes in XML using XSLT

Posted on 2008-10-27
6
290 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now