homepage Welcome to WebmasterWorld Guest from 50.16.112.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
XSLT to remove few Rows form the XML
sotu



 
Msg#: 4469820 posted 8:23 pm on Jun 26, 2012 (gmt 0)

Hello,

Currently I have following XML:

<Rowsets>
<Rowset>
<Row>

<DrilldownDepth>0</DrilldownDepth>
<ScheduleWeek>---</ScheduleWeek>

<ABC>---</ABC>
<PQR>1500</PQR>

<XYZ>15000</XYZ>
<Quant>285</Quant>

</Row>
<Row>
<DrilldownDepth>1</DrilldownDepth>

<ScheduleWeek>12</ScheduleWeek>
<ABC>---</ABC>

<PQR>1500</PQR>
<XYZ>15000</XYZ>

<Quant>285</Quant>
</Row>
<Row>

<DrilldownDepth>2</DrilldownDepth>
<ScheduleWeek>12</ScheduleWeek>

<ABC>SUPER</ABC>
<PQR>100</PQR>

<XYZ>200</XYZ>
<Quant>300</Quant>

</Row>
<Row>
<DrilldownDepth>2</DrilldownDepth>

<ScheduleWeek>12</ScheduleWeek>
<ABC>Duper</ABC>

<PQR>100</PQR>
<XYZ>200</XYZ>

<Quant>300</Quant>
</Row>
<Row>

<DrilldownDepth>2</DrilldownDepth>
<ScheduleWeek>12</ScheduleWeek>

<ABC>Fun</ABC>
<PQR>100</PQR>

<XYZ>200</XYZ>
<Quant>300</Quant>

</Row>
<Row>
<DrilldownDepth>2</DrilldownDepth>

<ScheduleWeek>7</ScheduleWeek>
<ABC>SUPER</ABC>

<PQR>100</PQR>
<XYZ>200</XYZ>

<Quant>300</Quant>
</Row>
<Row>

<DrilldownDepth>2</DrilldownDepth>
<ScheduleWeek>7</ScheduleWeek>

<ABC>Duper</ABC>
<PQR>100</PQR>

<XYZ>200</XYZ>
<Quant>300</Quant>

</Row>
<Row>
<DrilldownDepth>2</DrilldownDepth>

<ScheduleWeek>8</ScheduleWeek>
<ABC>SUPER</ABC>

<PQR>100</PQR>
<XYZ>200</XYZ>

<Quant>300</Quant>
</Row>
</Rowset>

</Rowsets>

Now I need only those <Row> where <DrilldownDepth>2</DrilldownDepth> and <ScheduleWeek>12</ScheduleWeek>.

So I have written following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:java="http://xml.apache.org/xslt/java" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="java" version="1.0">
<xsl:output media-type="text/xml" method="xml"/>
<xsl:variable name="depth">2</xsl:variable>
<xsl:variable name="week">12</xsl:variable>
<xsl:template match="/Rowsets">
<Rowsets>
<xsl:for-each select= "Rowset[ Row[DrilldownDepth=$depth and ScheduleWeek=$week] ]">


<xsl:copy-of select="."/>
</xsl:for-each>
</Rowsets>
</xsl:template>
</xsl:stylesheet>


But it is not giving me desired output. Can anyone help me with that?

Also the values that I am passing in XSLT 2 and 12, how can I make it dynamic to pass from outside?

 

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4469820 posted 8:46 pm on Jun 28, 2012 (gmt 0)

The task of dynamically generating XSLT with external settings is something best accomplished using a server-side language, such as Java or PHP or Perl or Python or whatever floats yer boat. PHP is actually quite good at it, since you merely need to add <?php echo $depth; ?> where you currently have the number "2"

In situations where the content of the XML isn't sufficient to render it the way I like, I usually abandon XSLT altogether and parse the XML using something like "SimpleXML" [php.net...]

The selective display of certain XML nodes is also easier to do using a server-side parsing, rather than an XSLT solution.

That said, maybe a more sophisticated XPATH in your for-each might be able to select rows where the depth is what you desire. in XSLT.

But that's not the solution I'd recommend.

As a general, personal rule - though I realize many XML/XSLT-focused apps break this rule - I insist that XML is not a *storage engine*. Storing piles of data in XML and treating it like a database is a mistake. Trying to craft the equivalent of a "where clause" in XSLT is not easy, because honestly XSLT isn't the right tool for that kind of thing.

XML is, at its best, a carrier format. Your XML input should contain only what you wish to display, and nothing more. Then the XSLT can be "dumb", and do what it does best - mark up and transform the XML into something that looks nice in an HTML browser.

That means you should re-examine your data access strategy. The data can be stored in a SQL database, and queried to get just the rows you desire. That data can then be assembled as XML, and then pass that XML into your XSLT stylesheet.

Don't take this personally, but it bothers me when people use a wrench to bang in a nail. Using XSLT to filter & sort data is evidence that XML is being used wrong.

Cheers
~ hww

Dijkgraaf

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4469820 posted 1:58 am on Jun 29, 2012 (gmt 0)

LoL httpwebwitch. Considering that converting XML using XSLT is my bread and butter in the integration space it made me smile. Admittedly I use BizTalk applications which usually takes care of creating the XSLT for me.
I do however agree that it is more efficient to extract only the data that is needed before creating the XML.

However regarding your issue Sotu if what you are getting is the XML and you've got no choice except to use that then first try having your logic constructed like the following and separate your select and your conditions.

<xsl:for-each select="Rowset/Row">
<xsl:if test="conditions">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>


As to being able to pass in parameters, that would probably depend on what you have executing the XSLT.

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4469820 posted 8:48 pm on Jul 10, 2012 (gmt 0)

If it's data that accumulates and grows, extracting only the needed data into XML is pretty much a requirement. Databases employ indexes (b-trees and such) to make queries fast. XML does not. When you have a 1-million line XML file, you'll see how true this is...

the biggest XML file I ever had to style up (with XSLT) was about 1.5Gb. It was the 8-minute page load times that convinced the client to pursue a more viable storage strategy

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved