Solved

Should I use XMLReader, XSLT or XPATH

Posted on 2010-09-08
6
332 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
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.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

896 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

14 Experts available now in Live!

Get 1:1 Help Now