 |

12-05-2007, 10:36 AM
|
 |
Moderator
|
|
Join Date: Jun 2006
Location: United States
Posts: 1,783
|
|
XSLT Conditionals
I am finally working on developing my first "pure" XML web site, mostly as an experiment. Trying to figure out how to convert the XML data files to XHTML using XSLT, I have found that there seem to be very few comprehensive tutorials on this technology, and I am hoping someone here will be able to get me past the first of what I am sure will be many stumbling blocks.
My question is, how do I test for a false condition? <xsl:if test="@name"> will execute if there is an attribute called name, but how can I get it to execute if name does not exist? The same thing applies with comparing values. If I want to check if name is not Tom, @name != 'Tom' does not work.
Also, as part of the same issue, is there a way to get an XSL template to output an invalid tag? Basically, if an element is found without the specified tag, I want to output a closing tag, followed by a new opening tag. I am trying to create a new row in a table when this happens. However, the closing tag causes the file to no longer be valid XML, and it doesn't process. Do I need to use a CDATA container or something else?
Here is my current, bad code.
Code:
<table><tr>
<xsl:for-each select="parameter">
<td><xsl:value-of select="@data" /></td>
<xsl:if test="@number != '3'">
</tr><tr>
</xsl:if>
</xsl:for-each>
</tr></table>
__________________
The best way to learn anything, is to question everything.
|

12-05-2007, 10:45 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,131
|
|
Re: XSLT Conditionals
Just worte about this today:
creating a .css file for xml - SitePoint Forums
Code:
<xsl:stylesheet version='1.0'
xmlns:xsl="http://w3.org/1999/XSL/Transform"
xmlns="http://w3.org/1999/xhtml">
<xsl:output method="xml" ... />
</xsl:stylesheet>
is the way to do it. There is no output method=xhtml as I know of.
Quote:
Originally Posted by wige
My question is, how do I test for a false condition? <xsl:if test="@name"> will execute if there is an attribute called name, but how can I get it to execute if name does not exist? The same thing applies with comparing values. If I want to check if name is not Tom, @name != 'Tom' does not work.
|
Generally you use this
axis::node test [predicats]
structure while filtering node sets. So you need to know the following concepts to perform tests: - Axes
- node test (name test / node type test)
- Predicates that filters the node set.
Quote:
Originally Posted by wige
Also, as part of the same issue, is there a way to get an XSL template to output an invalid tag? Basically, if an element is found without the specified tag, I want to output a closing tag, followed by a new opening tag. I am trying to create a new row in a table when this happens. However, the closing tag causes the file to no longer be valid XML, and it doesn't process. Do I need to use a CDATA container or something else?
|
CData shoul be able to do it. Depending on what you do (output method etc.) there can be problems with closing ?> etc. Last but not least, you need to know that white space (line feeds, carriage return, etc) may be interpreted different in various browsers. White space, you should know how that is handled from the very begining. It may be tricky.
Read more ...
Last edited by kgun : 12-05-2007 at 11:04 AM.
|

12-05-2007, 02:12 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,131
|
|
Re: XSLT Conditionals
Regarding my last remark.
To cite from may favourite PHP XML book, Robert Richards (2006): "Pro PHP XML and Web Services" from Apress page 354:
The name attribute, whose value is interpreted as an attribute value template, specifies the target of the Processing Instruction (PI) being created. The content of the element defines the data for the PI:
Code:
<xsl:processing-instruction name ="php">
print "Hello World";
</xsl:processing-instruction>
Caution: When the output method is HTML, processing instructions are terminated by > and not by ?>. This means you need manually add ? as part of the content of the xsl:processing-instruction element.
|

12-05-2007, 03:19 PM
|
 |
Moderator
|
|
Join Date: Jun 2006
Location: United States
Posts: 1,783
|
|
Re: XSLT Conditionals
Thanks for the information so far. I am (somewhat) getting the hang of it. I have a preliminary build of the site's first few pages up at TicketWarehouse.us, but I am finding certain components of this more tricky than others. For example, if I have html content in the xml file (such as inline elements like links) the processor ignores them. I have gone through stacks of tutorials but haven't been able to find anything that addresses topics beyond the very basics covered in the w3schools tutorials.
__________________
The best way to learn anything, is to question everything.
|

12-05-2007, 05:55 PM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,131
|
|
Re: XSLT Conditionals
I have mentioned one of my favourite books above. It is excellent. If you are going to be serious about this, you have to go to Amazon or another bookstore and buy some books, KW search: - XML family
- XSLT
- XPath, Xpointer
- XML Schema
Use the principle of progressive enhancement, from basic to more advanced. - XML markup.
- XSLT transformation.
- Validating using XML Schema.
- Icorporating external documents into your documents using XInclude.
- Semantic linking via XLink.
- DOM
- XML Parsers
The Document Object Model (DOM) is extremely important to know if you will use programs / scripts to parse your XML documents.
You can view a page online. You can view its source. The DOM is another method to view a page. Basically it maps the element, attributes, text nodes etc. onto a node tree of objects. You can manipulate the document by walking through the DOM tree, changing, deleting, inserting elements etc. You have complete control over the document by combining these technologies.
You find the most important technolgies here. It is technical, but the sooner you get used to searching for the right concepts, you should learn to use this site.
As an extra benefit, you will be able to make Web applications and AJAX much better after you handle the XML and DOM technologies.
Last edited by kgun : 12-05-2007 at 06:01 PM.
|

12-06-2007, 09:40 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,131
|
|
Re: XSLT Conditionals
Quote:
Originally Posted by Veikoh
XPath is not supported at all with browsers (IE 7, Opera 9), so I wasn't able to use it.
If element does not exists you should use apply-templates for that condition.
|
But you can use XPath before you transform your document to X(HT)ML or whatever document format using XSL(T) or XSL-FO or an external programming language.
Solution:
If there is no browser support for the XML technology, you can: - Transform your document using a member of the XML family like XSL(T). KW: Conditional import of style sheet.
- If not, use an external parser to do the transformation. You may need to write an test based on object detection, feature sniffing or event listeners if you use DOM or another parser.
Conclusion:
Let progressive enhancement be your guiding principle. Start simple and increase functionality and complexity.
Related link: XSL Operators >= and <= - SitePoint Forums
Last edited by kgun : 12-06-2007 at 10:01 AM.
|

12-06-2007, 10:14 AM
|
|
WebProWorld Member
|
|
Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
|
|
Re: XSLT Conditionals
The problem I tried to solve with XPath was that I had several <comment> tags for each <item> and I tried to get comments visually appear on separate WAP pages.
I added ID's for each comment but still wasn't able to format those with XSLT this way as specified with XPath.
If anyone have ideas how to implement this with XSLT I would be more than happy to learn.
Im unable to add any additional parsers as Im trying to run my app anywhere and was testing it on free UNIX server. I do not have any thousands of pounds project budgets to do something.
|

12-06-2007, 11:13 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,131
|
|
Re: XSLT Conditionals
Quote:
Originally Posted by Veikoh
The problem I tried to solve with XPath was that I had several <comment> tags for each <item> and I tried to get comments visually appear on separate WAP pages.
I added ID's for each comment but still wasn't able to format those with XSLT this way as specified with XPath.
|
Comment tags? Does that mean comment nodes?
Note: Comment nodes do not have an expanded name and are children of either the root (document in XPath 2.0) or the element nodes, but those are not parents of the comment nodes in XPath.
So if you are talking about comment (or processing instruction) nodes, those may be filtered with special filters in XPath.
Note:
Elements should be semantic, like links. Use descriptive and meaningful names for elements and dispaly content on your site using elements that contains information. Separate - Content (element tags).
- Presentation or styling (CSS and / or XSLT).
- (Dynamic) behaviour. Programming or scripting.
|

12-07-2007, 08:35 AM
|
|
WebProWorld Member
|
|
Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
|
|
Re: XSLT Conditionals
The XML code I had was following: <item><description>this is page one</description>
.....
<comment id=comment1>this should be on comment page one</comment>
<comment id=comment2>this should be on comment page two</comment>
.....
</item>
So when I tried to accsess comments by link http://www.xxc.com/item1.xml#comment1 as was described in W3C manual it did not show comment1 only. So I store now each comment on separate XML file.
Any ideas how I can I get comments appear on separate pages using XSLT and same XML file?
|

12-07-2007, 09:31 AM
|
 |
Moderator
|
|
Join Date: Jun 2006
Location: United States
Posts: 1,783
|
|
Re: XSLT Conditionals
The line should be <comment id="comment1">this should be on comment page one</comment> and you would access it with a test conditional
<xsl:choose>
<xsl:when test="@id='comment1'>
<xsl:apply-templates />
</xsl:when>
</xsl:choose>
Of course there will be additional decision logic in the template file to determine which comment to display based on the request url or some other parameter, but this example is just to show how to test for the attribute.
__________________
The best way to learn anything, is to question everything.
|

12-07-2007, 09:57 AM
|
 |
WebProWorld 1,000+ Club
|
|
Join Date: May 2005
Location: Norway
Posts: 5,131
|
|
Re: XSLT Conditionals
This is a typical XPointer (and XLink) problem. But the support for both technologies is not implemented in the major browsers as far as I know, but there are XML PHP libraries that can be used. Even some JavaScript libraries like jQuery (note the book Learning jQuery) has implemented XML functionality like XPath. Navigating, moving and manipulating the DOM tree is fairly easy once you understand these libraries.
Back to your problem.
Ideally the (future?) solution using XPointer is:
General syntax:
xpointer(xpath_expression)
Solution:
http://www.xxx.com/item1.xml#xpointer(id('comment1'))
That is a static HTML link. You can hoperfully soon make it semantic using XLink. And you can write XSLT transformations to transform XLinks to HTML links as long as the browsers are not XPointer and XLink enabled.
Last edited by kgun : 12-07-2007 at 10:08 AM.
|

12-10-2007, 09:41 AM
|
|
WebProWorld Member
|
|
Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
|
|
Re: XSLT Conditionals
As far I remember the problem was to pass a URL parameter to the XSLT file. Thats why I wasnt able to use any workarounds to implement conditional operations as I had a need to know, which comment was shown and which was the next one.
|

04-09-2008, 09:10 AM
|
|
WebProWorld Veteran
|
|
Join Date: Jul 2003
Location: Spain
Posts: 335
|
|
Re: XSLT Conditionals
I would be interested to know how you went on with your "pure XML site" - I am working on one at the moment, and my experience has been that, for basic listings (items for sale, etc), it seems to be OK (although not as straightforward as CSS), but for complex areas, like Admin, or members areas, requiring a lot of input, it just seems to add an unnecessary level of complication.
I usually write PHP/MySQL, with as much code / data separation as sensible via CSS.
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|