newbie XPath problem

Discussion in 'XML' started by Aidan, Dec 28, 2004.

  1. Aidan

    Aidan Guest

    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
     
    Aidan, Dec 28, 2004
    #1
    1. Advertising

  2. Aidan

    Joris Gillis Guest

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

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
     
    Joris Gillis, Dec 29, 2004
    #2
    1. Advertising

  3. Aidan

    Aidan Guest

    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:eek:...
    >> 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,
    >
    > --
    > Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
     
    Aidan, Dec 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,996
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,445
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    542
  4. goog
    Replies:
    0
    Views:
    518
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,664
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page