Subject: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.
Subject: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>
<!-- 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>
Subject: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.
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>
<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
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.
Subject: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.