XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
prasad ramaSubject: Printing non-existing value
Author: prasad rama
Date: 06 Feb 2009 08:02 PM
Hi,

I have the xml like this

<states>
<state abbr="AR" label="Arkansas">
<product code="DFRO" title="FIXED RATE OPTIONS FOR DISCONTINUED EQ. CREDITLINE"/>
<product code="EMS" title="HOME EQUITY LINE OF CREDIT (HELOC)"/>
<product code="HE2" title="HOME EQUITY LOAN"/>
</state>
<state abbr="AZ" label="Arizona">
<product code="DFRO" title="FIXED RATE OPTIONS FOR DISCONTINUED EQ. CREDITLINE"/>
<product code="FRO" title="FIXED RATE LOAN OPTIONS"/>
<product code="HE2" title="HOME EQUITY LOAN"/>
</state>
<state abbr="VA" label="Virginia">
<product code="DFRO" title="FIXED RATE OPTIONS FOR DISCONTINUED EQ. CREDITLINE" />
<product code="EMS" title="HOME EQUITY LINE OF CREDIT (HELOC)" />
<product code="FRO" title="FIXED RATE LOAN OPTIONS" />
<product code="HE2" title="HOME EQUITY LOAN" />
</state>
</states>

I need to the output like...

Arkansas
____________________________________
DFRO | EMS | FRO | None
____________________________________
Arizona
____________________________________
DFRO | None | FRO | HE2
____________________________________
Virginia
____________________________________
DFRO | EMS | FRO | HE2

But according to my xsl code..

<xsl:key name="product" match="/states/state/product" use="concat(../@abbr,@code)"/>
<xsl:template match="/">
<html>
<body>
<table border="1" width="100%" cellpadding="0" cellspacing="0">
<xsl:for-each select="/states/state">
<xsl:variable name="state" select="@abbr"/>
<tr>
<td colspan="5" align="center">
<xsl:value-of select="@label"/>
</td>
<tr>
<xsl:for-each select="//product[generate-id()=generate-id(key('product',concat($state,@code))[1])]">
<td>
<xsl:value-of select="@code"/>
</td>
</xsl:for-each>
</tr>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

I am getting like this...

DFRO | EMS | FRO
____________________________________
Arizona
____________________________________
DFRO | FRO | HE2
____________________________________
Virginia
____________________________________
DFRO | EMS | FRO | HE2


I need to print "None" value when the products are not exists in particular state.

Can any one tell me how to check the values, when the data was given like this.

Thank you,

Postnext
John BamptonSubject: Printing non-existing value
Author: John Bampton
Date: 07 Feb 2009 09:06 PM
Originally Posted: 07 Feb 2009 07:51 AM
[Webmaster: removed email]

This should work, its a bit verbose but works.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<html>
<head></head>
<body>
<table>
<xsl:for-each select="states/state">
<tr>
<td colspan="4"><xsl:value-of select="@label"></xsl:value-of></td>
</tr>
<tr>
<xsl:choose>
<xsl:when test="product[1]/attribute::code = 'DFRO' or product[2]/attribute::code = 'DFRO' or product[3]/attribute::code = 'DFRO' or product[4]/attribute::code = 'DFRO' ">
<td>DFRO</td>
</xsl:when>
<xsl:otherwise>
<td>NONE</td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="product[1]/attribute::code = 'EMS' or product[2]/attribute::code = 'EMS' or product[3]/attribute::code = 'EMS' or product[4]/attribute::code = 'EMS' ">
<td>EMS</td>
</xsl:when>
<xsl:otherwise>
<td>NONE</td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="product[1]/attribute::code = 'FRO' or product[2]/attribute::code = 'FRO' or product[3]/attribute::code = 'FRO' or product[4]/attribute::code = 'FRO' ">
<td>FRO</td>
</xsl:when>
<xsl:otherwise>
<td>NONE</td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="product[1]/attribute::code = 'HE2' or product[2]/attribute::code = 'HE2' or product[3]/attribute::code = 'HE2' or product[4]/attribute::code = 'HE2' ">
<td>HE2</td>
</xsl:when>
<xsl:otherwise>
<td>NONE</td>
</xsl:otherwise>
</xsl:choose>
<hr></hr>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Cheers. John Bampton.

Postnext
prasad ramaSubject: Printing non-existing value
Author: prasad rama
Date: 07 Feb 2009 07:06 PM
Hi John,

I really appreciate for your response. I provided only some part of the data from the XML file. The actual XML file is so big. It has so many states and products.

We can not take care of all the states and products with the code you provided, that means we can not expect states and their products.

So I we need to dynamically place the "None" values where we do not have the products.

Please give any suggestions.

Thanks once again.

Postnext
John BamptonSubject: Printing non-existing value
Author: John Bampton
Date: 08 Feb 2009 10:01 AM
Here try this solution, it uses SAXON.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
extension-element-prefixes="saxon"
xmlns:saxon="http://saxon.sf.net/">

<xsl:variable name="counter" select="1" saxon:assignable="yes"/>
<xsl:variable name="productcount" select="5"></xsl:variable>
<xsl:variable name="exists" select="0" saxon:assignable="yes"></xsl:variable>


<xsl:template match="/">
<html>
<head></head>
<body>
<table>
<xsl:for-each select="states/state">
<tr>
<td colspan="4"><xsl:value-of select="@label"></xsl:value-of></td>
</tr>
<tr>
<saxon:while test="$counter &lt; $productcount">
<xsl:choose>
<xsl:when test="product[$counter]/attribute::code = 'DFRO' ">
<td>DFRO</td>
<saxon:assign name="exists" select="1"/>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="$exists = 1 ">
<saxon:assign name="counter" select="$productcount"/>
</xsl:when>
<xsl:otherwise>
<saxon:assign name="counter" select="$counter+1"/>
</xsl:otherwise>
</xsl:choose>
</saxon:while>
<xsl:if test="$exists = 0">
<td>NONE</td>
</xsl:if>
<saxon:assign name="exists" select="0"/>
<saxon:assign name="counter" select="1"/>



<saxon:while test="$counter &lt; $productcount">
<xsl:choose>
<xsl:when test="product[$counter]/attribute::code = 'EMS' ">
<td>EMS</td>
<saxon:assign name="exists" select="1"/>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="$exists = 1 ">
<saxon:assign name="counter" select="$productcount"/>
</xsl:when>
<xsl:otherwise>
<saxon:assign name="counter" select="$counter+1"/>
</xsl:otherwise>
</xsl:choose>
</saxon:while>
<xsl:if test="$exists = 0">
<td>NONE</td>
</xsl:if>
<saxon:assign name="exists" select="0"/>
<saxon:assign name="counter" select="1"/>


<saxon:while test="$counter &lt; $productcount">
<xsl:choose>
<xsl:when test="product[$counter]/attribute::code = 'FRO' ">
<td>FRO</td>
<saxon:assign name="exists" select="1"/>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="$exists = 1 ">
<saxon:assign name="counter" select="$productcount"/>
</xsl:when>
<xsl:otherwise>
<saxon:assign name="counter" select="$counter+1"/>
</xsl:otherwise>
</xsl:choose>
</saxon:while>
<xsl:if test="$exists = 0">
<td>NONE</td>
</xsl:if>
<saxon:assign name="exists" select="0"/>
<saxon:assign name="counter" select="1"/>



<saxon:while test="$counter &lt; $productcount">
<xsl:choose>
<xsl:when test="product[$counter]/attribute::code = 'HE2' ">
<td>HE2</td>
<saxon:assign name="exists" select="1"/>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="$exists = 1 ">
<saxon:assign name="counter" select="$productcount"/>
</xsl:when>
<xsl:otherwise>
<saxon:assign name="counter" select="$counter+1"/>
</xsl:otherwise>
</xsl:choose>
</saxon:while>
<xsl:if test="$exists = 0">
<td>NONE</td>
</xsl:if>
<saxon:assign name="exists" select="0"/>
<saxon:assign name="counter" select="1"/>

<hr></hr>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Cheers, John Bampton

Postnext
John BamptonSubject: Printing non-existing value
Author: John Bampton
Date: 09 Feb 2009 06:44 AM
I can see an even faster way of doing it based on the code I just gave you.

Postnext
James DurningSubject: Printing non-existing value
Author: James Durning
Date: 09 Feb 2009 11:28 AM
The key is creating a temporary variable to the state node, and then getting all the code values.

<xsl:for-each select="/states/state">
<xsl:variable name="state" select="."/> <!-- create pointer to current node -->
...
<xsl:for-each select="//product[not(@code = preceding::product/@code)]/@code"><!-- get all unique codes -->
<td>
<xsl:choose>
<xsl:when test="$state/product[@code = current()]">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>None</xsl:otherwise>
</xsl:choose>
</td>
</xsl:for-each>

Postnext
prasad ramaSubject: Printing non-existing value
Author: prasad rama
Date: 09 Feb 2009 01:17 PM
Hi John,

I am using xml and xslt with .Net (dotNet). I can not use Saxon. Is while condition available in Xsl 1.0 version? Can you suggest me anyother alternative to solve this.

Thanks,

Hi James,

Thank you for replying to the post. I tried with your code snippet. It works properly with the data I posted above. But I added some more states & products and tried, it failed.

Thanks,

Postnext
John BamptonSubject: Printing non-existing value
Author: John Bampton
Date: 10 Feb 2009 03:00 AM
You can use Saxon.net which is a .net port of Saxon. That way you get support for XSLT/XPATH 2.0. .NET only supports XSLT 1.0.

Posttop
prasad ramaSubject: Printing non-existing value
Author: prasad rama
Date: 10 Feb 2009 07:58 PM
No, I can not use Saxon.

I have the data like this

{state abbr='TX'}
{product code="VA"}
{tier custom="240" ltv="0.01" min_amount="5000" rate="9.23" /}
{tier custom="240" ltv="10.01" min_amount="5000" rate="9.23" /}
{tier custom="240" ltv="20.01" min_amount="5000" rate="2.78" /}

{tier custom="240" ltv="0.01" min_amount="20000" rate="10.12" /}
{tier custom="240" ltv="10.01" min_amount="20000" rate="1.63" /}
{tier custom="240" ltv="20.01" min_amount="20000" rate="5.45" /}

{tier custom="240" ltv="30.01" min_amount="25000" rate="4.54" /}
{/product}
{product code=" "}
...............
...............
{/product}
{/state}

The output should look like this

Ltv(0) Ltv(10) Ltv(20)
Loan_amount(5000) 9.23 9.23 2.78

Loan_amount(20000) 10.12 1.63 5.45

Loan_amount(25000) None None 4.54

But with my code, I am getting the result like this

Ltv(0) Ltv(10) Ltv(20)
Loan_amount(5000) 9.23 9.23 2.78

Loan_amount(20000) 10.12 1.63 5.45

Loan_amount(25000) 4.54

Where the rate 4.54 should be in the 3rd column.

Here is my xsl..

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="lien" match="/states/state/product/tier" use="concat(../../@abbr,../@code,@min_custom)"/>
<xsl:key name="ltv" match="/states/state/product/tier" use="concat(../../@abbr,../@code,@min_custom,@min_loan_amount,@min_ltv)"/>
<xsl:key name="loan" match="/states/state/product/tier" use="concat(../../@abbr,../@code,@min_custom,@min_loan_amount)"/>
<xsl:template match="/">
<html>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<xsl:for-each select="/states/state[@abbr='TX']/product[@code='EMS']">
<xsl:for-each select="//tier[generate-id()=generate-id(key('lien',concat('TX','EMS','1'))[1])]">
<xsl:sort select="@min_custom" data-type="number" order="descending"/>
<xsl:variable name="custom" select="@min_custom"/>
<xsl:for-each select="../tier[generate-id()=generate-id(key('loan',concat('TX','EMS',$custom,@min_loan_amount))[1])]">
<xsl:sort select="@min_loan_amount" data-type="number" order="ascending"/>
<xsl:variable name="loan" select="@min_loan_amount"/>
<tr>
<td>
<xsl:value-of select="$loan"/>
</td>
<xsl:for-each select="../tier[generate-id()=generate-id(key('ltv',concat('TX','EMS',$custom,$loan,@min_ltv))[1])]">
<xsl:sort select="@min_ltv" data-type="number" order="ascending"/>
<td>
<xsl:value-of select="@rate"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

In the above Xsl code, I am looping thru the nodes which has distinct custom, loan_amount and ltv values. For the 3rd loan_amount i.e, 25000 , we do not have the data for ltvs 0 and 10. So that the rate belongs to loan_amount 25000 is printing in the 1st position. I need to print that in the 3rd position.

To do that I need to loop thru loan_amount 25000 for 3 times, So that I can check for the condition and place "None" value if no data exists. Right now It loops thru only for once and if place any condition, it is not working.

Please suggest me how to solve this issue.

Thanks,

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.