newbie XPath problem

A

Aidan

Hi,

I have an XML file I'm trying to prep for viewing through a web-browser
using XSL. I have sucessfully applied the stylesheet to the XML file, and
it displays, but I'm unable to properly test element values using
<xsl:choose> and <xsl:when> control structures.

Here's an example of what my XML file looks like:
========= XML =========
<zonecheck>
<domain>
<fqdn>fully-qualified-domain-name.com</fqdn>
<arecords>
<valid>false</valid>
</arecords>
<mxrecords>
<valid>true</valid>
<address>152.89.132.250</address>
</mxrecords>
<nsrecords>
<valid>false</valid>
</nsrecords>
</domain>
<domain>
.....
.....
</domain>
</zonecheck>
========= XML =========
And here is the stylesheet:
======Stylesheet=======
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" type="text/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>
<head>
<title>DNS report</title>
</head>

<body>

<table>

<tr>
<th>Domain</th><th>A Records</th><th>MX Records</th><th>NS Records</th>
</tr>

<xsl:for-each select="zonecheck/domain">
<xsl:sort select="fqdn" />

<tr>
<td><xsl:value-of select="fqdn" /></td>

<xsl:choose>
<xsl:when test="arecords/valid = 'true'">
<td bgcolor="00ff33"><xsl:value-of select="arecords/valid"
/></td>
</xsl:when>
<xsl:when test="arecords/valid = 'false'">
<td bgcolor="ff0033"><xsl:value-of select="arecords/valid"
/></td>
</xsl:when>
<xsl:eek:therwise>
<td><xsl:value-of select="arecords/valid" /></td>
</xsl:eek:therwise>
</xsl:choose>

<xsl:choose>
<!-- same structure as above for mxrecords -->
</xsl:choose>

<xsl:choose>
<!-- same structure as above for nsrecords -->
</xsl:choose>

</tr>

</xsl:for-each>

</table>

</body>
</html>

</xsl:template>

</xsl:stylesheet>
======Stylesheet=======

The file displays in Mozilla (I care less about IE), but it never contains
any of the extra colors that the <xsl:choose> structure is trying to add (in
other words, it always uses the <xsl:eek:therwise> option). As far as I can
tell, the problem is with my knowledge of XPath, and I'm wondering if some
XML genius out there can point out what I have done wrong in my stylesheet.

Thanks in advance...

Aidan
 
J

Joris Gillis

I have an XML file I'm trying to prep for viewing through a web-browser
using XSL. I have sucessfully applied the stylesheet to the XML file, and
it displays, but I'm unable to properly test element values using
<xsl:choose> and <xsl:when> control structures.
Hi,

With your - most likely simplified - XML input, the tests ared done correctly. The problem is probably related to whitespaces in your real XML.

Try to wrap normalize-space() around the node-set you're testing:
<xsl:when test="normalize-space(arecords/valid) = 'true'">


Btw, you don't have to repeat the the whole 'xsl:choose' structure every time, there are other ways to do that: you could use a template or an attribute-set. e.g. something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="html" indent="yes"/>

<xsl:template match="/">

<html>
<head>
<title>DNS report</title>
</head>

<body>

<table>

<tr>
<th>Domain</th><th>A Records</th><th>MX Records</th><th>NS Records</th>
</tr>

<xsl:for-each select="zonecheck/domain">
<xsl:sort select="fqdn" />
<tr>
<td>
<xsl:value-of select="fqdn" />
</td>
<td xsl:use-attribute-sets="style">
<xsl:value-of select="arecords/valid" />
</td>
</tr>
</xsl:for-each>

</table>

</body>
</html>

</xsl:template>

<xsl:attribute-set name="style">
<xsl:attribute name="bgcolor">
<xsl:if test="normalize-space(descendant::valid) = 'true'">00ff33</xsl:if>
<xsl:if test="normalize-space(descendant::valid) = 'false'">ff0033</xsl:if>
</xsl:attribute>
</xsl:attribute-set>

</xsl:stylesheet>

Finally, since the coloring problem is related to visual appearance, I'd rather recommend to use Cascading stylesheets. example: you could add a class attribute to the 'td' element with values 'valid' or 'unvalid' and leave the coloring (or more complex visual appearance) over to CSS.


regards,
 
A

Aidan

Hi Joris,

Thank you for your enlightening reply...

I've achieved the effect I was aiming for by using <xsl:attribute-set> to
create id attribute's in the <td> tags, that were assigned the value of
their respective <valid> elements, and then used these to change the
background color via CSS.

Thanks again... you really helped me alot (opened my eyes to a multitude of
ways to get things done).

Regards,

Aidan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top