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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is wrong with this code?

Status
Not open for further replies.

rachelason

Programmer
Jun 28, 2004
114
GB
Hello everyone!,

Though i am new to xml and xslt , i have managed to create an xml file. But when i try to display the information using xsl, it displays only one row of the data. As you can see from the xsl code, i have added <for-each tag aswell. I am not sure what the <xsl:template match >code does and i wonder if that's causing the problem. I have nearly 1000 rows in the xml, but it doesn't display if used with the following xsl. Any help?

Here is the code.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="<xsl:template match="/">
<html>
<body>
<h2>Tape Profile - File List</h2>
<table border="1">
<tr bgcolor="#006699">
<th align="left">FileName</th>
<th align="left">Fragment</th>

</tr>
<xsl:for-each select="profile">
<tr>
<td><xsl:value-of select="filename"/></td>
<td><xsl:value-of select="fragment"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

thanks
Rachel
 
And this is the content of the xml file

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="c:/xslfile.xsl"?>
- <profile>
<filename>0182F5E0</filename>
<fragment>0</fragment>
<filename>0182F6B0</filename>
<fragment>0</fragment>
.......
</profile>

Also, how important it is to have xml schema in the xml file? If it's an application(not internet related site), do i still have to have a schema to define the taglist.

Thanks
 
The corresponding lines.
[1]
[tt]<xsl:template match="profile">[/tt]
[2]
[tt] <xsl:for-each select="child::*[position() mod 2 = 1]">
<tr>
<td><xsl:value-of select="."/></td>
<td><xsl:value-of select="following-sibling::*[1]"/></td>
</tr>
</xsl:for-each>
[/tt]
 
I have managed to solve the problem,,, obviously the xml file didn't have a tag to seperate each block of code
<filename><fragment> should be inside a another tag.
thanks
 
>obviously the xml file didn't have a tag to seperate each block of code <filename><fragment> should be inside a another tag
You know, that is incorrect. You do what you like but the statement is wrong.
 
here is my code.... it seemed to work this way. Since i am new to xml, i tend to believe that that's how it has to be done. Sorry i don't under your first reply though.


<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="c:/xslfile.xsl"?>
<tape>
- <profile>

<filename>0182F5E0</filename>
<fragment>0</fragment>
<filename>0182F6B0</filename>
<fragment>0</fragment>
.......
</profile>
</tape>


xsl file

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="<xsl:template match="/">
<html>
<body>
<h2>Tape Profile - File List</h2>
<table border="1">
<tr bgcolor="#006699">
<th align="left">FileName</th>
<th align="left">Fragment</th>

</tr>
<xsl:for-each select="/tape/profile">
<tr>
<td><xsl:value-of select="filename"/></td>
<td><xsl:value-of select="fragment"/></td>

</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
 
>Sorry i don't under your first reply though.
You are question poster, you should change document to the form you understand. No reason to work with anything you don't understand.
 
I meant to write 'understand'
And i meant i don't understand the code you posted in reply to my question.
Anyway, the problem i had was solved. So don't worry.
 
rachelason,

In your penultimate post, your stylesheet does not match your XML document. tsuji posted a solution which will work with the XML document as posted both times. Since you don't understand the code, both he and I can explain it, line by line. "If you teach a man to fish..."

Here is the code tsuji is suggesting, with my comments added:
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="/">
  <html>
  <body>
    <h2>Tape Profile - File List</h2>
    <table border="1">
    <tr bgcolor="#006699">
      <th align="left">FileName</th>
      <th align="left">Fragment</th>
     
    </tr>
    [COLOR=blue]<!-- apply templates to the children of the root node
         which in this case means the profile element -->[/color]
    <xsl:apply-templates/>
    </table>
  </body>
  </html>
</xsl:template>
<xsl:template match="profile">
    [COLOR=blue]<!-- for each odd numbered (by document order position) 
         child element of the profile element -->[/color]
    <xsl:for-each select="child::*[position() mod 2 = 1]">
    [COLOR=blue]<!-- I think child::*[local-name() = 'filename']
         would work equally well -->[/color]
     <tr>
      [COLOR=blue]<!-- output the value of the filename element -->[/color]
      <td><xsl:value-of select="."/></td>
      [COLOR=blue]<!-- output the value of the immediately 
         following sibling (in document order) which is the
         fragment element -->[/color]
      <td><xsl:value-of select="following-sibling::*[1]"/></td>
     </tr>
    </xsl:for-each>
</xsl:template
</xsl:stylesheet>

I hope you find this solution to the problem you iriginally posted useful and informative.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top