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

Cant transform XML 1

Status
Not open for further replies.
Mar 9, 2011
2
US
I am trying to learn xml and xsl.
I have this simple xml:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<system>
<systemname>XP Common</systemname> 
<baseline>1.3</baseline> 
<systemip>192.168.1.100</systemip> 
<applications>
<appname>00012314-Apache</appname> 
<filename>apache.exe</filename> 
<version>2.0</version> 
<required>yes</required> 
<appname>V00012345-Microsoft Office PowerPoint</appname> 
<filename>POWERPNT.exe</filename> 
<version>12.0.6500.5000</version> 
<required>yes</required> 
<appname>V000123456-Adobe Acrobat Reader 9</appname> 
<filename>AcroRd32.exe</filename> 
<version>9.3.2.163</version> 
<required>yes</required> 
</applications>
</system>


and this xsl:
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>XML Text</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Custom App Name</th>
      <th>Filename</th>
	  <th>Version</th>
	  <th>Required</th>
    </tr>
	<tr>
    <xsl:for-each select="system/applications">    
		<td><xsl:value-of select="appname"/></td>
		<td><xsl:value-of select="filename"/></td>
		<td><xsl:value-of select="version"/></td>
		<td><xsl:value-of select="required"/></td>
	</xsl:for-each>
	</tr>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
I want to loop through EACH of the appnames and show their appname,filename, version, and required values in rows.
I can't see what I am doing incorrectly.
I have searched the tutorials - as a matter of fact, this is based on one I found online. Thank you.
 
well, if you can restructure your XML as follows
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<system>
<systemname>XP Common</systemname> 
<baseline>1.3</baseline> 
<systemip>192.168.1.100</systemip> 
<applications>
    [COLOR=red]<application>[/color]
        <appname>00012314-Apache</appname> 
        <filename>apache.exe</filename> 
        <version>2.0</version> 
        <required>yes</required> 
    [COLOR=red]</application>[/color]
    <application>
        <appname>V00012345-Microsoft Office PowerPoint</appname> 
        <filename>POWERPNT.exe</filename> 
        <version>12.0.6500.5000</version> 
        <required>yes</required> 
    </application>
    <application>
        <appname>V000123456-Adobe Acrobat Reader 9</appname> 
        <filename>AcroRd32.exe</filename> 
        <version>9.3.2.163</version> 
        <required>yes</required> 
    </application>
</applications>
</system>
note the 'application' (singular) tags
then you can use 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="/">
    <html>
    <body>
    <h2>XML Text</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th>Custom App Name</th>
    <th>Filename</th>
    <th>Version</th>
    <th>Required</th>
    </tr>
    <xsl:for-each select="system/applications/application">    
        [COLOR=red]<tr>[/color]
            <td><xsl:value-of select="appname"/></td>
            <td><xsl:value-of select="filename"/></td>
            <td><xsl:value-of select="version"/></td>
            <td><xsl:value-of select="required"/></td>
        [COLOR=red]</tr>[/color]
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
</xsl:stylesheet>
oh and I moved the 2nd set of 'tr' tags within the loop
 
Thank you for your assisstance JustinEzequiel.
I hope you realize that you really helped me out learning xml a little bit better today.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top