Solved

Should I use XMLReader, XSLT or XPATH

Posted on 2010-09-08
6
331 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

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

20 Experts available now in Live!

Get 1:1 Help Now