Link to home
Start Free TrialLog in
Avatar of AIBMass
AIBMass

asked on

Should I use XMLReader, XSLT or XPATH

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?
Avatar of grepll
grepll
Flag of Czechia image

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

Avatar of AIBMass
AIBMass

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of grepll
grepll
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AIBMass

ASKER

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.
Avatar of AIBMass

ASKER

Never mind. Thanks again.

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

http://support.microsoft.com/kb/300934
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