Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Include only few nodes in XML using XSLT

Posted on 2008-10-27
6
Medium Priority
?
314 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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).

721 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