homepage Welcome to WebmasterWorld Guest from 23.20.44.136
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Xml element name in XSLT
Need a little help
xcandyman




msg:701483
 1:26 pm on Mar 15, 2005 (gmt 0)

Hi

A begginer here with XML and XSLT and was wondering if anyone could help. I help many XML files but they are constructed in elements like

<category>
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</category>

How can I style this XML file with a XSLT file? I know how to do it if they wern't in elements but the "xsl:for-each select=" and "<xsl:value-of select=" dosn't seem to work.

Thank you

 

Alternative Future




msg:701484
 1:53 pm on Mar 15, 2005 (gmt 0)

Hello xcandyman,

Are your elements group by category?
<category>
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</category>
i.e. you have more than one category with children widgetname?

<xsl:for-each select="category">
<xsl:for-each select="element">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each>

Should loop through all categories and then each element and get the node value.

-George

xcandyman




msg:701485
 2:21 pm on Mar 15, 2005 (gmt 0)

Thanks george

The <category> should have really been <item> sorry to confuse things

This is how it's the code looks

<item>
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</item>

<item>
<element name="widgetname">big blue widgets2</element>
<element name="widget's in stock ">152</element>
<element name="widget location">in the widget house2</element>
</item>

etc

Here's how it I would like it to work which I managed to using the normal formating of xml but unfortunatly the xml files I'm working on has element name = which is really confusing me...


<table border="1">
<tr bgcolor="#9acd32">
<th>Widget Name</th>
<th>In Stock</th>
<th>Location</th>
</tr>
<xsl:for-each select="item">
<tr>
<td><xsl:value-of select="widgetname"/></td>
<td><xsl:value-of select="widgetsinstock"/></td>
<td><xsl:value-of select="widgetlocation"/></td>
</tr>
</xsl:for-each>
</table>

As you can see each <item> will be placed in a row of it's own until no more results are found.

Thanks again

Alternative Future




msg:701486
 2:27 pm on Mar 15, 2005 (gmt 0)

xcandyman,

There are many ways of capturing the node value of each element by using the xpath below should work for your needs:

<table border="1">
<tr bgcolor="#9acd32">
<th>Widget Name</th>
<th>In Stock</th>
<th>Location</th>
</tr>
<xsl:for-each select="item">
<tr>
<xsl:for-each select="element">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>

This will work as long as there are the same amount of elements for each title i.e. Widget Name etc.

Would this work for you? If not then please post back and I shall try another option...

-George

xcandyman




msg:701487
 2:37 pm on Mar 15, 2005 (gmt 0)

Thank you! It worked wonders!

Just one question though... Is it possible to create a <xsl:if?

Say if I only wanted to show only results under a specific group name?

<item>
<element name="group">WidgetGroup/element>
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</item>

<item>
<element name="group">AnotherWidgetGroup/element>
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</item>

Before I used.

<xsl:if test="WidgetGroup">
....Content....
</xsl:if>

<xsl:if test="AnotherWidgetGroup">
....Content....
</xsl:if>

Thanks again

Alternative Future




msg:701488
 2:47 pm on Mar 15, 2005 (gmt 0)

Do you have any control over the xml?

<item group="WidgetGroup">
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</item>

<item group="AnotherWidgetGroup">
<element name="widgetname">big blue widgets</element>
<element name="widget's in stock ">15</element>
<element name="widget location">in the widget house</element>
</item>

Would work like this:

<table border="1">
<tr bgcolor="#9acd32">
<th>Widget Name</th>
<th>In Stock</th>
<th>Location</th>
</tr>
<xsl:for-each select="item">
<xsl:choose>
<xsl:when test="@group= 'WidgetGroup'">
<tr>
<xsl:for-each select="element">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:when>
<xsl: otherwise>
..Else
</xsl: otherwise>
</xsl:choose>
</xsl:for-each>
</table>

If you do not have any control over the xml get back to me and I shall see what else I can come up with

-George

irnbru




msg:701489
 2:48 pm on Mar 15, 2005 (gmt 0)

Going back to your original data format you can change all those element nodes to use their meaningful names ...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:template match="/">
<xsl:element name="items">
<xsl:for-each select="category">
<xsl:element name="item">

<xsl:for-each select="element">

<xsl:variable name="theName">
<xsl:choose>
<xsl:when test='contains(@name,"widget location")'>
<xsl:text>widget-location</xsl:text>
</xsl:when>
<xsl:when test='contains(@name,"s in stock")'>
<xsl:text>widget-stock</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:element name="{$theName}">
<xsl:value-of select="."/>
</xsl:element>

<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Then the conversion from the newer format to your HTML might be more straight forward.

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<widgetname>big blue widgets</widgetname>
<widget-stock>15</widget-stock>
<widget-location>in the widget house</widget-location>
</item>
</items>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved