0
votes

I want to output the names, domain and usage of a database where its version is equal to 20i. I have created a stylesheet that does this - when I load my xml, it detects that I have an element matching my requirements in the table, but doesn't actually show them. No idea what I'm doing wrong!

XML:

    <?xml version="1.0" encoding="UTF-8" ?>
      <?xml-stylesheet type="text/xsl" href="xrt.xsl"?>
        <Inventory>
          <DatabaseName>
            <GlobalName>Tom</GlobalName>
            <Function>production</Function>
            <Domain>tom.info</Domain>
             <Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
             <Attributes Type="Production" Version="20ix"/>
             <Comments>
             ...
             </Comments>
             <Usage>
             500
             </Usage>
             </DatabaseName>

             <DatabaseName>
            <GlobalName>Ricardo</GlobalName>
            <Function>production</Function>
            <Domain>tom.info</Domain>
             <Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
             <Attributes Type="Production" Version="20i"/>
             <Comments>
             ...
             </Comments>
             <Usage>
             500
             </Usage>
             </DatabaseName>

             <WebserverName>
                <GlobalName>Jim</GlobalName>
                <Function>distribution</Function>
                <Domain>jim1235.com</Domain>
                 <Administrator EmailAlias="rkarvani" Extension="134237">Richard Karvani</Administrator>
                 <Administrator EmailAlias="stones" Extension="222237">Steve Jones</Administrator>
                 <Attributes Type="Production" Version="20i"/>
                 <Comments>
                 ...
                 </Comments>
                 <Usage>
                 1200
                 </Usage>
               </WebserverName>
             </Inventory>

XSLT Document:

            <?xml version="1.0" encoding="UTF-8"?>
            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
               <xsl:template match="/">
                  <html>
                     <body>
                        <h1>My Server List</h1>
                        <h2>Database Servers</h2>
                        <xsl:apply-templates select="Inventory/DatabaseName" />
                        <h2>Webservers</h2>
                        <xsl:apply-templates select="Inventory/WebserverName" />
                     </body>
                  </html>
               </xsl:template>

               <xsl:template match="DatabaseName">

                <table border="1">
                  <tr>
                    <th>Name</th>
                    <th>Domain</th>
                    <th>Usage</th>
                  </tr>
                  <xsl:for-each select="Attributes[@Version='20i']">
                  <tr>
                    <td><xsl:value-of select="GlobalName"/></td>
                    <td><xsl:value-of select="Domain"/></td>
                    <td><xsl:value-of select="Usage"/></td>
                  </tr>
                </xsl:for-each>
                </table>
               </xsl:template>



               <xsl:template match="WebserverName">
                    <table border="1">
                  <tr>
                    <th>Name</th>
                    <th>Domain</th>
                    <th>Usage</th>
                  </tr>
                  <xsl:for-each select="Attributes[@Version='20i']">
                  <tr>
                    <td><xsl:value-of select="GlobalName"/></td>
                    <td><xsl:value-of select="Domain"/></td>
                    <td><xsl:value-of select="Usage"/></td>
                  </tr>
                </xsl:for-each>
                </table>
               </xsl:template>

            </xsl:stylesheet>
1

1 Answers

0
votes

I think you want to put the condition about that version into the apply-templates and move the table creation to the another template:

<?xml version="1.0" encoding="UTF-8" ?>
            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
               <xsl:template match="/">
                  <html>
                     <body>
                        <h1>My Server List</h1>
                        <h2>Database Servers</h2>
                        <table border="1">
                            <tr>
                                <th>Name</th>
                                <th>Domain</th>
                                <th>Usage</th>
                            </tr>
                            <xsl:apply-templates select="Inventory/DatabaseName[Attributes[@Version='20i']]" />
                        </table>
                        <h2>Webservers</h2>
                        <table border="1">
                            <tr>
                                <th>Name</th>
                                <th>Domain</th>
                                <th>Usage</th>
                            </tr>
                            <xsl:apply-templates select="Inventory/WebserverName[Attributes[@Version='20i']]" />
                        </table>
                     </body>
                  </html>
               </xsl:template>

               <xsl:template match="DatabaseName">                

                  <tr>
                    <td><xsl:value-of select="GlobalName"/></td>
                    <td><xsl:value-of select="Domain"/></td>
                    <td><xsl:value-of select="Usage"/></td>
                  </tr>

               </xsl:template>



               <xsl:template match="WebserverName">

                  <tr>
                    <td><xsl:value-of select="GlobalName"/></td>
                    <td><xsl:value-of select="Domain"/></td>
                    <td><xsl:value-of select="Usage"/></td>
                  </tr>

               </xsl:template>

            </xsl:stylesheet>