xsl-list
[Top] [All Lists]

Re: XSL problem.

2005-05-26 13:48:56
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(_at_)hotmail(_dot_)com> 
05/26/2005 03:16 PM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com


To
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
cc

Subject
[xsl] 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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>