Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display specific elements of XML using XSL

Status
Not open for further replies.

lovinasd

Programmer
Jan 23, 2005
23
US
Hi,
I have a simple XML which is autogeneraated.So the nodes are dynamic and some system information nodes like sequence,client id etc is static and fixed notes.
I would like to display all nodes except theses static nodes like sequence and client.can we have a sort adn match creteria in xsl using 'like or wild cards'

eg:
the static elements start with S and C so how can i rewrite the following xsl .Current i ahve to change the xsl and write all node which i want to display
for example if i want to display the below mentioned nodes
AppName,Name and SerialNumber and not static nodes like starting with S or C (They also lies in the element node)


<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>AppName</th>
<th>Name</th>
<th>Serial Number</th>
</tr>
<xsl:for-each select="Report/Element">
<xsl:sort select="Name"/>
<tr>
<td><xsl:value-of select="App_Name"/></td>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Serial Number"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Could you please tell me what is the error in the above XSL
thanks
 
How about "SerialNumber"? It starts with S. Is it system and static?
 
Serial Number is dynamic lets assume
elements starting with sa or ca
thanks
 
Show the forum a shortened sample so that some might help. Say, why Reoirt/Element? what are they and how thing structured? Are they then dynamic?... so vague to warrant a serious undertaking.
 
The xml looks like this
<Report>
<Element>
<App_Name>ttttt</App_Name>
<Name>yyyyy</Name>
<SerialNumber>88888</SerialNumber>
<sa2>iiii</sa2>
<sa3>eeeee</sa3>
<ca4>sss</ca333>
</Element>

<Element>
<App_Name>aaat</App_Name>
<Name>qqqqy</Name>
<SerialNumber>333333888</SerialNumber>
<sa2>fff</sa2>
<sa3>ggggee</sa3>
<ca4>sgggggs</ca333>
</Element>
</Report>

each row has a element tag and The static nodes are SerialNumber App_Name Name..
but elements like sa2,sa3,ca4 are dynamic in sense that they are generated from a problem upon which depends the name of nodes they can be ca3, ca7 anythg.
If i need to match element and display all starting with ca or sa and not qa from a xml how can i sort the xml using the xsl.I have provided the current xsl being used in the last replies in the forum.
Thanks
 
I would probably do something along the lines of this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates select="Report"/>
  </xsl:template>
  <xsl:template match="Report">
    <html>
      <body>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>AppName</th>
            <th>Name</th>
            <th>Serial Number</th>
            <th>Other Nodes</th>
          </tr>
          <xsl:for-each select="Element">
            <xsl:sort select="Name"/>
            <tr>
              <td>
                <xsl:value-of select="App_Name"/>
              </td>
              <td>
                <xsl:value-of select="Name"/>
              </td>
              <td>
                <xsl:value-of select="SerialNumber"/>
              </td>
              <td>
                <table border="1">
                  <tr bgcolor="#9acd32">
                    <xsl:apply-templates select="*[position() &gt; 3][substring(name(.), 1, 2) = 'ca' or substring(name(.), 1, 2) = 'sa'][substring(name(.), 1, 2) != 'qa']" mode="th"/>
                  </tr>
                    <tr>
                      <xsl:apply-templates select="*[position() &gt; 3][substring(name(.), 1, 2) = 'ca' or substring(name(.), 1, 2) = 'sa'][substring(name(.), 1, 2) != 'qa']" mode="td"/>
                    </tr>
                </table>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="node()" mode="th">
    <th>
      <xsl:value-of select="name(.)"/>
    </th>
  </xsl:template>
    <xsl:template match="node()" mode="td">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>
Although you could probably make it more concise/dynamic with more info on how you want to sort/display the data.

Jon

"I don't regret this, but I both rue and lament it.
 
Thanks ,
itwas a great help.i will check it out.
 
The file works fine but .The elements don't match
each row keeps getting off one column
can u tell me why
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top