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
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 25 Oct 2005 11:05 AM
Im trying to display n number of article sections
from an xml file using xsl.

I want to select all the section nodes
then loop through the types one node at a time
so when the node type matchs the correct
formatting is used.
however im not having any luck i keep getting
errors on one of the line.

<xsl:if test="boolean(product_sheet/section)">
lookie
<xsl:for-each select="child::product_sheet/section">
beep
<xsl:if test = "product_sheet/child::section==listgroup">
beep2
<xsl:for-each select="//listgroup/list">
<li><xsl:value-of select="current()"/></li>
<br>here</br>
</xsl:for-each>
<br></br>
</xsl:if>
</xsl:for-each>
</xsl:if>

Beep 2 is not showing.
the xpath listing for list is
product_sheet/section/listgroup/list.

What am I doing wrong? or am i totally going the wrong way about this?

Postnext
(Deleted User) Subject: Xpath problem
Author: (Deleted User)
Date: 25 Oct 2005 11:09 AM
Can you publish your XML file ?

Postnext
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 25 Oct 2005 11:15 AM

Its still a bit rough, here all 3 reference files.
xml file

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style_template.xsl"?>
<!-- programmer: mary gilmartin. date: oct 05 -->
<product_sheet xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="example.xsd">
<id>203</id>
<title>45030405.doc</title>
<type>cleaning chemicals</type>
<subtype>floor</subtype>
<banner>altova.gif</banner>
<main_heading>PRODUCT DATA SHEET</main_heading>
<product_name>DYCLEAN</product_name>
<subheading>Product Description</subheading>

<section>
<link>http://www.google.ie</link>
<listgroup>
<list>An advanced cationic cleaning solution for cleaning areas where bacterial control is not an issue.</list>
<list>Odourless, heavy-duty cleaner and degreaser.</list>
<list>Use for periodic of all Dycem products.</list>
<list>Concentrated mix for dilution with water.</list>
</listgroup>
</section>
<subheading>Product Properties</subheading>
<section>
<tableb>
<row>
<col>Parameter</col>
<col>Specification</col>
</row>
<row>
<col>General appearance</col>
<col>Cobalt Blue Mobile Liquid</col>
</row>
<row>
<col>Odour</col>
<col>Negligible</col>
</row>
<row>
<col>PH @20C</col>
<col>12.50-13.50</col>
</row>
<row>
<col>Solubility in Water</col>
<col>Soluble- mix as per recommended dilution</col>
</row>
</tableb>
<image>blah.gif</image>
<listgroup>
<list>Five Litre container</list>
<list>2 x 5 litre container per case</list>
</listgroup>
<note></note>
</section>

<tlink>http://www.yahoo.com</tlink>
<mail>quote@atp.com</mail>
</product_sheet>
<?xml-stylesheet type="text/xsl" href="style_template.xsl"?>

--------------------------------------
Schema

<?xml version="1.0" encoding="UTF-8" ?>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
- <!-- programmer: mary gilmartin. date: oct 05
-->
- <!-- definition of page complex element
-->
- <xs:element name="product_sheet">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <!-- document header elements
-->
<xs:element name="type" type="xs:string" />
<xs:element name="subtype" type="xs:string" />
<xs:element name="id" type="xs:string" />
<xs:element name="title" type="xs:string" />
<xs:element name="banner" />
<xs:element name="main_heading" type="xs:string" />
<xs:element name="product_name" type="xs:string" />
<xs:element name="subheading" type="xs:string" />
- <!--
section element, tables, paragraphs, images
lists, links, mailto

-->
- <xs:element name="section">
- <xs:complexType mixed="true">
- <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="paragraph" type="xs:string" />
- <xs:element name="listgroup">
- <xs:complexType mixed="true">
- <xs:sequence>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="list" type="xs:string" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
- <!-- tables i intend to border
-->
- <xs:element name="tableb">
- <xs:complexType>
- <xs:sequence>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="row">
- <xs:complexType>
- <xs:sequence>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="col" type="xs:string" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
- <!-- tables i intend to leave without borders
-->
- <xs:element name="tablenb">
- <xs:complexType>
- <xs:sequence>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="row">
- <xs:complexType>
- <xs:sequence>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="col" type="xs:string" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="image" type="xs:string" />
<xs:element name="note" type="xs:string" />
<xs:element name="link" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
- <!-- document footer elements
-->
<xs:element name="mail" type="xs:string" />
<xs:element name="tlink" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
------------------------------------------------------
xsl file

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- programmer: mary gilmartin. date: oct 05. company:ATP -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="product_sheet/main_heading"/></title>
</head>
<body bgcolor='white'>

<!-- Generalised code for outputting images (img not showing) -->
<img scr="{product_sheet/banner}"/>

<h2><xsl:value-of select="product_sheet/main_heading"/></h2>
<h3><xsl:value-of select="product_sheet/product_name"/></h3>
<br><b><li><xsl:value-of select="product_sheet/subheading"/></li></b></br>

<!-- Attempt at generalised sections
if there is section tags show the section elements
need to iterate through the nodes to place
them in the order the xml file declares them in -->
<xsl:if test="boolean(product_sheet/section)">
lookie
<xsl:for-each select="child::product_sheet/section">
beep
<xsl:if test = "product_sheet/child::section==listgroup">
the first child in the section nodetree is a list
<xsl:for-each select="//listgroup/list">
<li><xsl:value-of select="current()"/></li>
<br>here</br>
</xsl:for-each>
<br></br>
</xsl:if>
</xsl:for-each>
</xsl:if>


<!-- old code -->
<br></br>
<!-- Need to generalised paragraph location -->
<xsl:for-each select="product_sheet/section/listgroup/list">
<li><xsl:value-of select="current()"/></li>
<br></br>
</xsl:for-each>
<br></br>

<!-- generic links -->
<a>
<xsl:attribute name = "href">
<xsl:value-of select = "product_sheet/section/link"/>
</xsl:attribute>
<xsl:value-of select = "product_sheet/section/link"/>
</a>

<br><b><li><xsl:value-of select="product_sheet/subheading[2]"/></li></b></br>

<!-- Generalised code for outputting any table with a border-->
<table border='1' cellpadding='2'>
<xsl:for-each select="product_sheet/section/tableb/row">
<tr>
<xsl:for-each select="col">
<td>
<xsl:value-of select="current()"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
<br></br>

<!-- Generalised code for outputting any borderless table -->
<table border='0' cellpadding='2'>
<xsl:for-each select="product_sheet/section/tablenb/row">
<tr>
<xsl:for-each select="col">
<td>
<xsl:value-of select="current()"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
<br></br>

<!-- Need to generalised paragraph location -->
<xsl:for-each select="product_sheet/section[2]/listgroup/list">
<li><xsl:value-of select="current()"/></li>
<br></br>
</xsl:for-each>
<br></br>

<!-- generic links outside section blocks-->
<a>
<xsl:attribute name = "href">
<xsl:value-of select = "product_sheet/tlink"/>
</xsl:attribute>
<xsl:value-of select = "product_sheet/tlink"/>
</a>
<br></br>

<!-- generic mailto -->
<a>
<xsl:attribute name = "href">mailto:
<xsl:value-of select = "product_sheet/mail"/>
</xsl:attribute>
<xsl:value-of select = "product_sheet/mail"/>
</a>
<br></br>

</body>
</html>
</xsl:template>
</xsl:stylesheet>
-------------------------------

Postnext
(Deleted User) Subject: Xpath problem
Author: (Deleted User)
Date: 25 Oct 2005 11:34 AM
You probably want to replace "==" with "=" in
<xsl:if test="product_sheet/child::section==listgroup">

Postnext
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 25 Oct 2005 11:36 AM
That just creates

XSLT 1.0 Debugging Error: Invalid XPath expression
in XMLSpy.

Postnext
(Deleted User) Subject: Xpath problem
Author: (Deleted User)
Date: 25 Oct 2005 12:03 PM
You actually want to have this line :

<xsl:if test="local-name(child::*[1])='listgroup'">

if you want to have a condition "first child element of the section is listgroup"

because within this loop :

<xsl:for-each select="child::product_sheet/section">

your current node will be 'section'

We've executed your XSLT using msxml, Saxon and Xalan J, and .NET processors. All of them generated HTML page.

We are Stylus Studio team, not Altova XML Spy team, and we do not provide any support for Altova XML Spy.

Postnext
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 25 Oct 2005 12:22 PM
Originally Posted: 25 Oct 2005 12:09 PM
Oh Im not looking for support (Im sure my problem is code) just xsl/xpath advise. The page generates due to old code. but the new code wasnt showing as beep2 wasnt coming up. I'll try out the new line. . . . Beep2 still isnt showing.

Postnext
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 25 Oct 2005 12:20 PM
Ok maybe my problem is logical.

If you have a parent node <section>
with 4 possible child nodes
<image><listgroup><paragaph><table>.

And in the xml document
a <section> can contain any of the child
nodes in any number or any order.

I want to format the nodes
but keep the order they are in,
in the xml file. (data driven xsl)

so logical i want to test first
is there anything in the section.

then looping through the different
<section nodes> with a for-each

I test each child node as i find it
to see what type of node it is.

If its <listnode>
i display it by looping through
the <list> nodes and adding html tags.

Is this good practice?
If it is then my code isnt matching
what im trying to do.
If it isnt, then my method is flawed.

Postnext
(Deleted User) Subject: Xpath problem
Author: (Deleted User)
Date: 25 Oct 2005 01:55 PM
Creating XML from your test case we will have something like this :

<?xml version="1.0"?>
<section>
<image>Image1</image>
<image>Image2</image>
<listgroup>Listgroup1</listgroup>
<paragraph>Paragraph1</paragraph>
<paragraph>Paragraph2</paragraph>
<listgroup>Listgroup1</listgroup>
<paragraph>Paragraph3</paragraph>
<table>Table</table>
<p>This is some text which will stay unchanged</p>
</section>

Suggested XSLT will look like :


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>

<xsl:template match="/">
<html>
<head>
<title>Sample XSLT</title>
</head>
<body bgcolor="white">
<!-- We will iterate over direct children of section -->
<xsl:for-each select="section/*">
<!-- Current node "." here is section child -->
<xsl:apply-templates select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>

<!-- If this is "image" we will create HTML list item with
image text using red color -->
<xsl:template match="image">
<font color="red">
<li>
<xsl:value-of select="text()"/>
</li>
</font>
</xsl:template>

<!-- If this is "listgroup" we will create HTML list item with
image text using green color -->
<xsl:template match="listgroup">
<font color="green">
<li>
<xsl:value-of select="text()"/>
</li>
</font>
</xsl:template>


<!-- If this is "paragraph" we will create HTML list item with
image text using blue color -->
<xsl:template match="paragraph">
<font color="blue">
<li>
<xsl:value-of select="text()"/>
</li>
</font>
</xsl:template>

<!-- If this is "table" we will create HTML list item with
image text using yellow color -->
<xsl:template match="table">
<font color="yellow">
<li>
<xsl:value-of select="text()"/>
</li>
</font>
</xsl:template>

<!-- For all other elements we will do full copy -->
<xsl:template match="*">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

Now let's take a look why in your case this condition

<xsl:if test="local-name(child::*[1])='listgroup'">

never evaluates into "true".

Let's take first case where it can potentially match :

<section>
<link>http://www.google.ie</link>
<listgroup>
<list>An advanced cationic cleaning solution for cleaning areas where bacterial control is not an issue.</list>
<list>Odourless, heavy-duty cleaner and degreaser.</list>
<list>Use for periodic of all Dycem products.</list>
<list>Concentrated mix for dilution with water.</list>
</listgroup>
</section>

in this case "listgroup" is the second child element of "section" and
condition evaluates into "false". If you change XML this way :

<section>
<listgroup>
<list>An advanced cationic cleaning solution for cleaning areas where bacterial control is not an issue.</list>
<list>Odourless, heavy-duty cleaner and degreaser.</list>
<list>Use for periodic of all Dycem products.</list>
<list>Concentrated mix for dilution with water.</list>
</listgroup>
<link>http://www.google.ie</link>
</section>

"listgroup" becomes first child element of "section" and you'll get your <li> output.


Postnext
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 26 Oct 2005 04:17 AM
Thanks that highlighted my problem.
Ill work with this information
and see can i fix it.
The way you used templates might really help.

Postnext
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 26 Oct 2005 04:48 AM
Is it possible to apply-templates from a template?

for example

if i did this
<xsl:for-each select="root_node/*">
<!-- go to all the children of the root node-->
<xsl:apply-templates select="."/>
</xsl:for-each>

<xsl:template match="child_of_root">
<!-- apply templates to the children of child_or_root -->
<xsl:for-each select="child_of_root/*">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>

in order for the top level nodes to be matchedv in order
and the system to match the children of the lower level nodes
as it went along.

like:

a
b
b1
b2
c

or would it be better to instead of matching the
children of the root node, matching the
descendants of the root node.

What would be the different in display order?

Posttop
Mary GilmartinSubject: Xpath problem
Author: Mary Gilmartin
Date: 26 Oct 2005 07:27 AM
Originally Posted: 26 Oct 2005 07:10 AM
Its possible. Thank you. Its working as I want it too now.

 
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.