Solved

Should I use XMLReader, XSLT or XPATH

Posted on 2010-09-08
6
335 Views
Last Modified: 2012-05-10
Unfortunately I'm new to XML and am not sure if I should be pursuing XMLReader, XSLT or XPATH to solve the following easy problem. Could someone please suggest and possibly demonstrate a good approach? Comments on why you chose that approach would make this even more valuable.

Simplified XML file:

<beginningstuff>.................</beginningstuff>
<row><stuff1>....<stuff1><from>A</from><morestuff>....</morestuff>....</row>
<row><stuff1>....<stuff1><from>B</from><morestuff>....</morestuff>....</row>
<row><stuff1>....<stuff1><from>A</from><morestuff>....</morestuff>....</row>
<row><stuff1>....<stuff1><from>A</from><morestuff>....</morestuff>....</row>

I want to create an output XML document that contains the "beginningstuff" and only those "rows" that are "from" B. (In this case, that's just one row.)

What's the best way?
0
Comment
Question by:AIBMass
[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

Expert Comment

by:grepll
ID: 33629223
I would suggest using XSLT, this technology is well suited to your problem. See example below, it copies root tag, beginningstuff and rows with from containing B.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/*">
	<xsl:copy>
		<xsl:copy-of select="beginningstuff | row[from = 'B']"/>
	</xsl:copy>
</xsl:template>

</xsl:stylesheet>

--- Input file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<beginningstuff>.................</beginningstuff>
<row><stuff1>....</stuff1><from>A</from><morestuff>....</morestuff>....</row>
<row><stuff1>....</stuff1><from>B</from><morestuff>....</morestuff>....</row>
<row><stuff1>....</stuff1><from>A</from><morestuff>....</morestuff>....</row>
<row><stuff1>....</stuff1><from>A</from><morestuff>....</morestuff>....</row>
</root>

Open in new window

0
 

Author Comment

by:AIBMass
ID: 33629343
I'm going to look at this soon.

Is this adaptable to:
1. Choosing the rows that are not from 'B', including those that don't have a 'from'?
2. What if the valid values of 'from' need to be read from a config file?

Thanks.
0
 
LVL 3

Accepted Solution

by:
grepll earned 500 total points
ID: 33630320
1. Yes, you would modify the X-Path expression to: "beginningstuff | row[not(from) or from != 'B']"

2. Yes, if the config file is XML.
You can use function document() to access this additional file. See below for two solutions, both stylesheets expect file "config.xml" with allowed values of "from".
The former is more readable, but slower if you have many allowed values of "from".
The latter using key is faster, but slighty harder to understand.
--- XSLT without key
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/*">
	<xsl:copy>
		<xsl:copy-of select="beginningstuff"/>
		<xsl:for-each select="row">
			<xsl:if test="document('config.xml')/from/allowed[. = current()/from]">
				<xsl:copy-of select="."/>
			</xsl:if>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>

</xsl:stylesheet>

--- XSLT using key
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="allowed_from" match="/from/allowed" use="text()"/>

<xsl:template match="/*">
	<xsl:copy>
		<xsl:copy-of select="beginningstuff"/>
		<xsl:for-each select="row">
			<xsl:variable name="row" select="."/>
			<xsl:for-each select="document('config.xml')">
				<xsl:if test="key('allowed_from', $row/from)">
					<xsl:copy-of select="$row"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>

</xsl:stylesheet>

--- config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<from>
	<allowed>A</allowed>
	<allowed>C</allowed>
</from>

Open in new window

0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:AIBMass
ID: 33637392
Thank you grepll for your help.

Perhaps one last tidbit?

We receive an XML document from an outside source, which I can now envision transforming via an XSLT. But how does one accomplish this transfornation from inside a Windows app and end up with a new document that can be saved?

Best.
0
 

Author Comment

by:AIBMass
ID: 33637489
Never mind. Thanks again.

We will be using XSLTransform for others wishing to know - documented at this link:

http://support.microsoft.com/kb/300934
0
 
LVL 3

Expert Comment

by:grepll
ID: 33637527
You're welcome.

> But how does one accomplish this transfornation from inside a Windows app and end up with a new document that can be saved?

That depends. One from many possible solutions is to use SAXON XSLT processor. That is library for Java and .NET and could be also used as standalone application from commandline:

java -jar saxon9he.jar -novw -o output.xml input.xml stylesheet.xsl
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shredding xml into an oracle 11g Database 2 91
XML SQL 8 32
TSQL XML Namespaces 7 54
.NET XML to Dictionary - XML has repeating node names 8 28
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.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

734 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