Solved

Using XSLT, how to extract a certain element from a file and discard the rest

Posted on 2014-11-13
7
158 Views
Last Modified: 2014-11-13
I have an XML file and I want to extract a particular element (eee) and any attributes it may have, and discard the rest of the file.
For example if my input XML file was this:

<?xml version="1.0"?>
<aaa>
      <bbb>
            <ccc>...</ccc>
            <ccc>...</ccc>
      </bbb>
      <ddd>
            <eee f="1" g="2"/>
            <eee f="3" g="4"/>
  </ddd>
</aaa>            

I would want to end up with this:
            <eee f="1" g="2"/>
            <eee f="3" g="4"/>
            
The format of the XML data may change and I will not necessarily know that but I will always only want the eee element and any attributes it may have.

Can anyone point this XSLT newbie in the right direction!
0
Comment
Question by:Letterpart
  • 4
  • 2
7 Comments
 
LVL 32

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 40440490
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/*">
		<html>
			<head>
				<title>Matches</title>
			</head>
			<body>
				<table border="1">
					<xsl:apply-templates select="//eee"/>
				</table>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="eee">
		<tr>
			<td>
				<xsl:copy>
					<xsl:apply-templates select="@*|node()"/>
				</xsl:copy>
			</td>
		</tr>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 40440533
@ste5an, I don't think  html transformation is the requirement (and you are loosing attributes)

you can use copy-of

<xsl:template match="/">
<xsl:copy-of select="//eee"/>
</xsl:template>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40440602
there are some things you need to take into account
serialisation should be XML
<xsl:output method="xml" version="1.0" />
and you might need to wrap a root element arround it
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Closing Comment

by:Letterpart
ID: 40440719
Thanks ste5an and Geert. Both solutions worked for me (and outputted attributes).
I have split the points - hope that is OK with you.
ste5an was first but Geert's solution seems to be slightly neater in that there is one template instead of two.
Thanks for your help.
0
 
LVL 32

Expert Comment

by:ste5an
ID: 40440816
@Geert, had only IE at hand to do the transform 😉
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40440869
Hi,

First let me say that I don't mind at all that you split the points, I am OK with that

But I fail to see how ste5an's solution could possibly work
-  I am not aware of a single XSLT processor that accepts <xsl:output method="html" version="1.0" ...
you simply can't serialize html to version 1.0
- by doing <xsl:apply-templates select="@*" you push out the attributes and a built in template picks up the text nodes in it, so you will get
           <td>
               <eee>12</eee>
            </td>
in text mode, which is not
<eee f="1" g="2"/>
(héhé, Ste5an actually uses 3 built in (invisible) templates, one for '/4, one for '@*' and one for 'text()' but that is not bad at all :-)

Anyhow, if you are happy, I am, just wanted to make that point

Stating that my solution is slightly neater because it has only one template is a bit weird.
It is neater because it does what you asked,
but XSLT developers consider code neater when it has a good functional seperation... mostly code that has more templates is considered neater :-)
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40440875
@ste5an, I understand :-)
(aha, that one ignores the serialisation settings, hence no error on the version)
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

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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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