XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Peter DiricksonSubject: XML Transformation selecting some nodes
Author: Peter Dirickson
Date: 04 Mar 2005 01:16 PM

Hello folks,

I have this XML:

<?xml version="1.0" encoding="utf-8"?>
<matrix id="2" name="Matrix 2" title="">
<header>
<product pro_id="1" pro_name="Sony VAIO" pro_image="item_1.jpg" />
<product pro_id="2" pro_name="Toshiba" pro_image="item_2.jpg" />
<product pro_id="3" pro_name="Dell" pro_image="item_3.jpg" />
<product pro_id="4" pro_name="Acer" pro_image="item_4.jpg" />
</header>
<link link_id="5" linkname="Display">
<cell pro_id="1">15.4"</cell>
<cell pro_id="2">15"</cell>
<cell pro_id="3">15.4"</cell>
<cell pro_id="4">17"</cell>
</link>
<link link_id="6" linkname="Memory">
<cell pro_id="1">512</cell>
<cell pro_id="2">512</cell>
<cell pro_id="3">512</cell>
<cell pro_id="4">1024</cell>
</link>
<link link_id="4" linkname="Weigth">
<cell pro_id="1">9.2</cell>
<cell pro_id="2">7.8</cell>
<cell pro_id="3">7.9</cell>
<cell pro_id="4">8.0</cell>
</link>
</matrix>


I need to select some IDs (different everytime) and display the results below,
for example IDs = 1, 2, 4

Product Sony VAIO Toshiba Acer
Image item_1.jpg item_2.jpg item_4.jpg
----------------------------------------------------------
Display 15.4" 15" 17"
Memory 512 512 1024
Weigth 9.2 7.8 8.0


or another case IDs = 1

Product Sony VAIO
Image item_1.jpg
-------------------------
Display 15.4"
Memory 512
Weigth 9.2


Thanks a lot for any help or ideas.

Sincerely,

Peter.

Posttop
Ivan PedruzziSubject: XML Transformation selecting some nodes
Author: Ivan Pedruzzi
Date: 15 Mar 2005 08:53 AM

In XSLT 2 you can write an elegant solution taking advantage
of sequence data type


<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:param name="selection" select="(1, 2, 4)"/>

<xsl:variable name="p" select="/matrix/header/product"/>
<xsl:variable name="c" select="/matrix/link/cell"/>

<xsl:template match="/">

<xsl:text>Products: </xsl:text>
<xsl:for-each select="$selection">
<xsl:variable name="id" select="."/>
<xsl:value-of select="$p[@pro_id = $id]/@pro_name"/>
<xsl:text> </xsl:text>
</xsl:for-each>

<xsl:text>
Images: </xsl:text>
<xsl:for-each select="$selection">
<xsl:variable name="id" select="."/>
<xsl:value-of select="$p[@pro_id = $id]/@pro_image"/>
<xsl:text> </xsl:text>
</xsl:for-each>

<xsl:text>
------------------------------------
</xsl:text>
<xsl:for-each select="/matrix/link">

<xsl:for-each select="cell">
<xsl:variable name="id" select="@pro_id"/>
<xsl:variable name="prod_model" select="."/>

<xsl:if test="position() = 1 and $selection[. = $id]">
<xsl:value-of select="../@linkname"/><xsl:text> </xsl:text>
</xsl:if>

<xsl:for-each select="$selection[. = $id]">
<xsl:value-of select="$prod_model"/>
<xsl:text> </xsl:text>
</xsl:for-each>

</xsl:for-each>
<xsl:text>
</xsl:text>

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

hope this helps
Ivan

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.