PDA

View Full Version : Need help with XSLT and element vs. attribute decisions.



moveon
Oct 1st, 2002, 08:49 AM
Hello everyone.

I am new to XML, and here I am trying to construct some kind of catelog modelling a shop.

If I put the link of a picture as an attribute in my XML file, how would I transform it into an attribute of an image tag with XSLT. So I would like to render my XML file into XSLT. This is something I have:

<product imgsrc="1.jpg">
<Name>Crap</Name>
...
</product>

And I would like to take this link, "1.jpg", and put it as an image in the xhtml file generated by XSLT.

In addition, I have no idea of what to put as element and what to put as attribute. This is a rough list of things that I have for my XML:

catelog, product (w/ ID, name, imgsrc, description, category, availability, width/height i.e. dimension)

ID would be an attribute to the product tag, but what about category and availability? It seems that avail. can be denoted by a number, say 0 and 1, and the user doesn't necessarily need to know what the code stands for. As name is an element, show category be an element too?

Finally, should width/height be elements of the dimension tag, as elements of just the product tag, or width/height be attributes to the dimension tag (which is an element of product)?

Thank you very much, and I look foward to some suggestions.

mpjbrennan
Oct 1st, 2002, 09:01 AM
Hi,

If you are that new to XML/XSLT can I suggest that you visit the Learn XSL tutorials at http://www.w3schools.com/ You'll learn so much there in an hour or so that might take days of posting questions.

good luck

patrick

moveon
Oct 1st, 2002, 09:28 AM
Thanks for the link. Just so anyone wants to try this, here is the code I wrote:

test.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<img src="carter.jpg" />

test.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/img">
<html>
<head><title>Testing</title></head>
<body>
<img>
<xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
</img>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Ugly indentation here... xsl:attribute does the magic!