[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSL problem.

Subject: Re: XSL problem.
From: JBryant@xxxxxxxxx
Date: Thu, 26 May 2005 15:48:56 -0500
winks html xml
Greetings, and welcome to the world of XSLT.

You have hit one of those problems that demands the use of the oft-reviled 
// operator. We avoid it for most purposes because it has tremendous 
overhead (because it walks every node in the source document), but this 
problem can use it well.

Here's one solution (but I didn't take the time to try to replicate the 
exact HTML you want):

<?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>
      <table border="1">
        <tr><th>Element</th><th>Attributes</th></tr>
        <xsl:for-each select="//*">
          <tr><td><xsl:value-of select="name()"/></td>
          <xsl:for-each select="@*">
            <td><xsl:value-of select="name()"/><xsl:if 
test="not(position() = last())">, </xsl:if></td>
          </xsl:for-each>
          </tr>
        </xsl:for-each>
      </table>
    </body>
  </html>
</xsl:template>
 
</xsl:stylesheet>

And here's the result of running that stylesheet against itself (which 
works because an XSL stylesheet is an XML document):

<html>
   <body>
      <table border="1">
         <tr>
            <th>Element</th>
            <th>Attributes</th>
         </tr>
         <tr>
            <td>xsl:stylesheet</td>
            <td>version</td>
         </tr>
         <tr>
            <td>xsl:template</td>
            <td>match</td>
         </tr>
         <tr>
            <td>html</td>
         </tr>
         <tr>
            <td>body</td>
         </tr>
         <tr>
            <td>table</td>
            <td>border</td>
         </tr>
         <tr>
            <td>tr</td>
         </tr>
         <tr>
            <td>th</td>
         </tr>
         <tr>
            <td>th</td>
         </tr>
         <tr>
            <td>xsl:for-each</td>
            <td>select</td>
         </tr>
         <tr>
            <td>tr</td>
         </tr>
         <tr>
            <td>td</td>
         </tr>
         <tr>
            <td>xsl:value-of</td>
            <td>select</td>
         </tr>
         <tr>
            <td>xsl:for-each</td>
            <td>select</td>
         </tr>
         <tr>
            <td>td</td>
         </tr>
         <tr>
            <td>xsl:value-of</td>
            <td>select</td>
         </tr>
         <tr>
            <td>xsl:if</td>
            <td>test</td>
         </tr>
      </table>
   </body>
</html>

If you want only the distinct values (that is, no duplicates), you've got 
a different (but not too much harder) problem. Let us know if that's the 
case. Still, I bet the //* trick will get you started. Just remember to 
always use more-specific paths when you can, to avoid the processing 
overhead of //.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies).




"Frequent Fliers" <frequent_fliers@xxxxxxxxxxx> 
05/26/2005 03:16 PM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
 XSL problem.






Hi firstly im really new to xsl + xpath. im using xsl on windows xp so im 
not sure what processor it is
:(
anyway, im trying to write an xsl file that will print out every element 
name in an xml file as well as the attributes.
Presently im trying to print out the name of every element.i have the 
following:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version 
= 
"1.0" >
<xsl:template match="/">
<html>
  <body>
    <fieldset>
      <legend>
        <font face="verdana,arial,helvetica" size="+3" color="#00006B">
            <xsl:apply-templates />
        </font>
      </legend>
    </fieldset>
      <xsl:apply-templates />
  </body>
</html>
</xsl:template>
  <xsl:template match="*">
    <xsl:value-of select="name()" />
  </xsl:template>
  <xsl:template match="*">
    <xsl:for-each select=".">
      <xsl:value-of select="name()" />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
the first template prints out the root node name, which works.but for the 
second template all i get is the name of the root node again, but just in 
plain text, so its the second template.
here is my xml file.
<book>
<author age="" id="">
    <fisname/>
    <lastname/>
</author>
</book>
Please be aware that im trying to write the xslt file to work generically 
for any xml file.so im not aware of the names of any of the elements, 
attributes or the presence of attributes for that matter.
Please help, im really stuck!
thank you very much.!

_________________________________________________________________
Winks & nudges are here - download MSN Messenger 7.0 today! 
http://messenger.msn.co.uk

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.