kwindo Posted October 23, 2010 Share Posted October 23, 2010 Hi there,I'm trying convert xml from an estates webservice to xhtml. In the XML there are elenments like : when an estate has a garden<Garden>1</Garden>or when an estate has no garden<Garden i:nil="true"/> when an estate has no gardenI am transforming the xml data to an html table.I need to show a <tr> line with "yes" in a cell when the value of "Garden" is "1". And the <tr> line to not show when the "Garden" tag is like this <Garden i:nil="true"/>. Can anyone help me with this? Link to comment Share on other sites More sharing options...
Martin Honnen Posted October 24, 2010 Share Posted October 24, 2010 Well we need to see enough of the XML input to know which namespaces are defined. So assuming you have <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Garden>1</Garden> <Garden xsi:nil="true"/></Root> you can write code like this <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsi"> <xsl:template match="Garden[. = 1]"> <tr> <td>yes</td> </tr> </xsl:template> <xsl:template match="Garden[@xsi:nil = 'true']"/></xsl:stylesheet> Link to comment Share on other sites More sharing options...
boen_robot Posted October 24, 2010 Share Posted October 24, 2010 You'll first need to declare the "i" namespace URI in your XSLT. I'm assuming that's the XML Schema Instance namespace, so the URI should be "http://www.w3.org/2001/XMLSchema-instance". The prefix within the XSLT is irrelevant, as long as the URI matches the one in the XML.After that, it's a matter of simply detecting the nil attribute, and assume "no" if it's there (or if the contents of <Garden> says "0"), and "yes" otherwise (I'd think the reason they have <Garden> with a number is in case the estate has more than one garden, so you may want to also say "yes" on two and more gardens), so: <xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:i="http://www.w3.org/2001/XMLSchema-instance"...exclude-result-prefixes="i">...<td><xsl:choose><xsl:when test="@i:nil = 'true' or .='0'">no</xsl:when><xsl:otherwise>yes</xsl:otherwise></xsl:choose></td>...</xsl:stylesheet> Link to comment Share on other sites More sharing options...
kwindo Posted October 24, 2010 Author Share Posted October 24, 2010 Hej, thanks a lot for your fast replies.I figured it out last night a couple of hours after I made my post but your replies made me thinking again.I'm very new to xslt and parsing xml and even developing. Now I'm a designer but I used to be a cook ;-)I had some problem figuring out this "namespaces" stuff. To be honest it's still a blurr to me but I got my stuff working so far.I get this root tag in the xml:<EstateServiceGetEstateListResponse xmlns="something.somethingelse" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">(It doesn't say "something.somethingelse" but I signed an agreement to not post company info on the web so I'm just being carefull ;-)I figured out how to make my xsl work by adding a prefix to the "xmlns="something.somethingelse" at the top of my xsl like this...<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kwd="something.something">(I just made up the "kwd")...and then decalaring that prefix in my paths. Like this:<xsl:value-of select="kwd:Garden"/>Asuming the "Garden" will only contain a "1" or "nil" I came up with this:<xsl:if test="kwd:Garden = 1"> <tr> <td class="estatetitlecll">Garden:</td> <td class="estatecll"><xsl:text>Yes</xsl:text></td> </tr></xsl:if>I've been trying to find estates with more then one "Garage" sinds it's more obvious then more then one "Garden" but I can only find "1", "0" or "nill" in the xml . So my solution seems ok for me, for now (I have to check with that company if "more then one" is possible in these cases.)My solution is so simple I doupting it's the right way to do this. Any reason why I shouldn't do it like this? Link to comment Share on other sites More sharing options...
boen_robot Posted October 24, 2010 Share Posted October 24, 2010 If you then have another if that could generate a no, and that if checks for the nil attribute, then consider the (unlikely, but technically possible) case where there IS a nil attribute AND content. By using <xsl:choose> (as by my previous post) or a template match (as demonstrated by Martin), you'll treat this case as either yes or no, but not both, which would not make sence.But if they are smart enough to use nil on the first place, then they are smart enough not to cause that unlikely scenario above, so... well, you can still do it for efficiency's sake. Why check for a "no", if "yes" is guaranteed by the first portion? That's what <xsl:choose> is for. Link to comment Share on other sites More sharing options...
kwindo Posted October 24, 2010 Author Share Posted October 24, 2010 While searching for estates with more then one garage I found that their output is not very concistant since "no garage" can be "nill" or "0". I'm expecting some debugging after I made my programming attempt ready for presentation. I'm sure your explination on <xsl:choose> will come in very handy. Thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.