PDA

View Full Version : where wrong, and how to solve


xiaodao
10-13-2004, 12:52 AM
1.xml
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="1.xsl"?>
<project>
<file>
<name>name1</name>
<url>link</url>
</file>
<file>
<name>name1</name>
<url>link</url>
</file>
<file>
<name>name1</name>
<url>link</url>
</file>
<file>
<name>name1</name>
<url>link</url>
</file>
</project>

1.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="personnel/file">

<script>document.write "<a href="+<xsl:value-of select=artist>+">"+<xsl:value-of select=name>+"</a>"</script>


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

mpjbrennan
10-13-2004, 09:46 AM
There are a number of errors in your code, for example:
1. The stylesheet references "personnel/file" - the XML document has no nodes of this type.
2. The stylesheet references "artist" - the XML document has no nodes of this type.
3. The stylesheet does not have a closing </xsl:stylesheet> tag
4. The stylesheet is named "1.xsl" - xsl is not a recognised mime-type (IE lets you get away with this)

To achieve what you want I would not bother with javascript - just use XSL. In the example below I have rewritten your data to show what I mean. Hope this helps.

Patrick

XML file
--------

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="1_xsl.xml"?>
<project>
<file>
<name url="link1">name1</name>
</file>
<file>
<name url="link2">name2</name>
</file>
<file>
<name url="link3">name3</name>
</file>
<file>
<name url="link4">name4</name>
</file>
</project>


XSL file
-------

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</xsl:template>

<xsl:template match="file">
<xsl:for-each select="name">
<p>
<a style="color:blue">
<xsl:attribute name="href"><xsl:value-of select="./@url"/></xsl:attribute>
<xsl:value-of select="."/>
</a>
</p>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

xiaodao
10-13-2004, 05:36 PM
it works, but this part i dont understand can explain
<xsl:for-each select="name">
<p>
<a style="color:blue">
<xsl:attribute name="href"><xsl:value-of select="./@url"/></xsl:attribute>
<xsl:value-of select="."/>
</a>
</p>
</xsl:for-each>

mpjbrennan
10-13-2004, 06:10 PM
Hi xiaodao

1. The <xsl:for-each> statement cycles through all the <name> elements

2. Ignore the style="color:blue" in the <a> tag - it's just my favourite colour

3. The <xsl:attribute> statement creates a link for each <name> tag with an href equal to the value of the url attribute in the name tag.

4. The <xsl:value-of select="."> prints the value of the current node

If you prefer to have the url information within its own tag (as in your original example) then you could do it like this:

XML file
------------
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="1_xsl.xml"?>
<project>
<file>
<name>name1</name>
<url>link1</url>
</file>
<file>
<name>name2</name>
<url>link2</url>
</file>
<file>
<name>name3</name>
<url>link3</url>
</file>
<file>
<name>name4</name>
<url>link4</url>
</file>
</project>

XSL file
------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</xsl:template>

<xsl:template match="file">
<xsl:for-each select="name">
<p>
<a style="color:blue">
<xsl:attribute name="href"><xsl:value-of select="../url"/></xsl:attribute>
<xsl:value-of select="."/>
</a>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

best regards

Patrick

xiaodao
10-13-2004, 11:40 PM
guru, u really good, thanks for help