homepage Welcome to WebmasterWorld Guest from 54.227.77.237
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 fetch desired Rows and the same time change node value
Xslt,xml
sotu




msg:4480365
 11:38 pm on Jul 30, 2012 (gmt 0)

My Current XML is following:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2012-07-17T11:57:07" EndDate="2012-07-17T11:57:07" StartDate="2012-07-17T10:57:07" Version="12.0.12 Build(9)">
<Rowset>
<Row>
<CategoryID>1</CategoryID>
<Name>Philip</Name>
<City>London</City>
<Phone>123</Phone>
</Row>
<Row>
<CategoryID>2</CategoryID>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<CategoryID>3</CategoryID>
<Name>Bruke</Name>
<City>LosAngeles</City>
<Phone>600</Phone>
</Row>
<Row>
<CategoryID>2</CategoryID>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
<Row>
<CategoryID>3</CategoryID>
<Name>Cristina</Name>
<City>SanJose</City>
<Phone>890</Phone>
</Row>
<Row>
<CategoryID>4</CategoryID>
<Name>Meredith</Name>
<City>Sunnyvale</City>
<Phone>788</Phone>
</Row>
<Row>
<CategoryID>4</CategoryID>
<Name>Grey</Name>
<City>MountainView</City>
<Phone>456</Phone>
</Row>
<Row>
<CategoryID>5</CategoryID>
<Name>Torrence</Name>
<City>SAntaClara</City>
<Phone>432</Phone>
</Row>
</Rowset>
</Rowsets>


Now I want only those <Row> whose <CategoryID> is 2. So my XSLT is following:

<?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"/>
<!-- Filters refdoc based on condition and data -->
<xsl:template match="/">
<Rowsets>
<xsl:for-each select="/Rowsets/Rowset">
<Rowset>
<xsl:copy-of select="Columns"/>
<xsl:for-each select="Row[CategoryID = '2']">
<xsl:copy-of select="."/>
</xsl:for-each>
</Rowset>
</xsl:for-each>
</Rowsets>
</xsl:template>
</xsl:stylesheet>


This XSLT gives me following XML:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
<Row>
<CategoryID>2</CategoryID>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<CategoryID>2</CategoryID>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
</Rowset>
</Rowsets>


But now my requirement is that in the above new transformed XML, I also want to change CategoryID as 1.

SO my final XML should look like this:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
<Row>
<CategoryID>1</CategoryID>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<CategoryID>1</CategoryID>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
</Rowset>
</Rowsets>


I'm not sure how can this be achieved in an optimal way using same XSLT. Can anyone help me with it?

 

Dijkgraaf




msg:4480382
 1:34 am on Jul 31, 2012 (gmt 0)

<?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"/>
<!-- Filters refdoc based on condition and data -->
<xsl:template match="/">
<Rowsets>
<xsl:for-each select="/Rowsets/Rowset">
<Rowset>
<xsl:copy-of select="Columns"/>
<xsl:for-each select="Row[CategoryID = '1'] | Row[CategoryID = '2']">
<xsl:copy-of select="."/>
</xsl:for-each>
</Rowset>
</xsl:for-each>
</Rowsets>
</xsl:template>
</xsl:stylesheet>

sotu




msg:4480701
 8:58 pm on Jul 31, 2012 (gmt 0)

Well, unfortunately this XSLT wont serve the purpose.

From, original XML, I need all <Row> where CategoryID is 2 only. But this category ID "2" should be converted to 1 in the transform XML.

Your XSLT gives me <Row> with category ID "1" or "2" which is not my requirement.

Dijkgraaf




msg:4480717
 10:03 pm on Jul 31, 2012 (gmt 0)

Ok, sorry I misread your requirements.
Rather than using <xsl:copy-of select="."/> you need to select the individual elements and write your own Category ID

e.g.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<xsl:apply-templates select="/Rowsets" />
</xsl:template>
<xsl:template match="/Rowsets">
<Rowsets>
<Rowset>
<xsl:for-each select="Rowset/Row">
<xsl:if test="CategoryID = '2'">
<Row>
<CategoryID>
<xsl:text>1</xsl:text>
</CategoryID>
<Name>
<xsl:value-of select="Name/text()" />
</Name>
<City>
<xsl:value-of select="City/text()" />
</City>
<Phone>
<xsl:value-of select="Phone/text()" />
</Phone>
</Row>
</xsl:if>
</xsl:for-each>
</Rowset>
</Rowsets>
</xsl:template>
</xsl:stylesheet>

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