Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > newbie XPath problem

Reply
Thread Tools

newbie XPath problem

 
 
Aidan
Guest
Posts: n/a
 
      12-28-2004
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"
xmlnssl="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>
<xsltherwise>
<td><xsl:value-of select="arecords/valid" /></td>
</xsltherwise>
</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 <xsltherwise> 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


 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      12-29-2004
> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
 
Reply With Quote
 
 
 
 
Aidan
Guest
Posts: n/a
 
      12-30-2004
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


"Joris Gillis" <> wrote in message
news...
>> 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
>
> <xslutput 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,
>
> --
> Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57