 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 8th, 2012, 07:02 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Test Condition not working in xslt.
I have written xslt for generating the insert into statement but in my xml thr are some nodes which are optional. In my node attribute is also present,so i have given two choose condition for the node as well for the attribute which contains test and otherwise condition for inserting null if node is not present and inserting the value if that node & attrubute is coming.But, i am getting value in my output but not the null when node is not present.Plz suggest where my xslt is wrong,xml and xslt are pasted below:
XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test1>
<test2>
<test3>
<ABC>2012012</ABC>
<test4 />
<test4 />
<test4 />
<test4 />
</test3>
</test2>
</test1>
<test1>
<test2>
<test3>
<ABC>2012012</ABC>
<test4>
<DEF ghi="EA">-100.0</DEF>
</test4>
<test4>
<DEF ghi="EA">-100.0</DEF>
</test4>
<test4>
<DEF ghi="EA">-100.0</DEF>
</test4>
</test3>
</test2>
</test1>
</root>
XSLT:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" media-type="text/csv" method="text"/>
<xsl:template match="/">
<root>
<xsl:for-each select="//test1">
<xsl:for-each select="test2">
<xsl:for-each select="test3">
<xsl:variable name="ABC" select="ABC"/>
<xsl:for-each select="test4">
<xsl:variable name="DEF">
<xsl:choose>
<xsl:when test="not(DEF)">Null</xsl:when>
<xsl:otherwise>
<xsl:value-of select="DEF"/> </xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="ghi">
<xsl:choose>
<xsl:when test="DEF/@ghi='' or not(DEF/@ghi)">Null</xsl:when>
<xsl:otherwise>
<xsl:value-of select="DEF/@ghi"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat("INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( '" ,
$DEF,"'" ,",", "'" ,
DEF/@ghi,"'" ,",", "'" ,
$ABC, "'", ")", ";" )"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Last edited by darkknight; August 9th, 2012 at 04:46 AM..
|
|

August 8th, 2012, 09:16 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You seem to be declaring a variable named $DEF which you then don't use - I don't see any reference to $DEF in your stylesheet.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 9th, 2012, 01:43 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
Thanks for the reply!!!!.
I have decalred the variable DEF(Node) & ghi(attribute of DEF node) in my stylesheet and then i am retrieving the values of both of them in the insert into statement in the stylesheet.My aim is to get the "null" if the node and attribute are not present in the xml and the respective values if they are present.
Please do let me know if i have muddled anything.
|
|

August 9th, 2012, 01:53 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Perhaps you have written "DEF" when you intended "$DEF".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 9th, 2012, 04:45 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
Thanks!!!! ,now i am getting the "null" value when my node DEF is not present but still i am not getting the "null" in the attribute(ghi) of that node. I tried some syntax but i am still not getting the null values.Could you please suggest what should be the syntax for the attribute so that i get null values in my query.
Current output of the XSLT is mentioned below for your reference:
INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( 'Null','','2012012');INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( 'Null','','2012012');INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( 'Null','','2012012');INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( 'Null','','2012012');INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( '-100.0','EA','2012012');INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( '-100.0','EA','2012012');INSERT INTO tablename (DEF,ghi,ABC ) VALUES ( '-100.0','EA','2012012');
|
|

August 14th, 2012, 01:40 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Still not getting "null" for the attribute(ghi) .Please Help
|
|

August 14th, 2012, 04:27 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well you haven't shown us your new XSLT, so it is hard to say what you are doing wrong as we're not mind readers. If you have replaced "DEF/@ghi" with $ghi in the output part of your XSLT then it should work.
|
|

August 14th, 2012, 06:31 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Sam,
Thanks a lot!!!! ,I am getting the desired output.
|
|
 |