Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Should I use XMLReader, XSLT or XPATH

Posted on 2010-09-08
6
Medium Priority
?
338 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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…

610 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