WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-05-2007, 10:36 AM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,783
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default 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.
Reply With Quote
  #2 (permalink)  
Old 12-05-2007, 10:45 AM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,131
kgun RepRank 3kgun RepRank 3
Default 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 View Post
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:
  1. Axes
  2. node test (name test / node type test)
  3. Predicates that filters the node set.
Quote:
Originally Posted by wige View Post
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.
Reply With Quote
  #3 (permalink)  
Old 12-05-2007, 02:12 PM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,131
kgun RepRank 3kgun RepRank 3
Default 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.
Reply With Quote
  #4 (permalink)  
Old 12-05-2007, 03:19 PM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,783
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default 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.
Reply With Quote
  #5 (permalink)  
Old 12-05-2007, 05:55 PM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,131
kgun RepRank 3kgun RepRank 3
Default 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:
  1. XML family
  2. XSLT
  3. XPath, Xpointer
  4. XML Schema
Use the principle of progressive enhancement, from basic to more advanced.
  1. XML markup.
  2. XSLT transformation.
  3. Validating using XML Schema.
  4. Icorporating external documents into your documents using XInclude.
  5. Semantic linking via XLink.
  6. DOM
  7. 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.
Reply With Quote
  #6 (permalink)  
Old 12-06-2007, 09:10 AM
WebProWorld Member
 

Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
Veikoh RepRank 0
Default Re: XSLT Conditionals

I had also lot of same kind of conditional problems when I wrote my WAP platform - Developing mobile web publishing platform using XML, XSLT and Perl. « Veiko Herne – lifetime entrepreneur with adventurous soul
In some cases I got it work using choose instead of if. Somehow everything worked much better in Opera than IE.
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.
Reply With Quote
  #7 (permalink)  
Old 12-06-2007, 09:40 AM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,131
kgun RepRank 3kgun RepRank 3
Default Re: XSLT Conditionals

Quote:
Originally Posted by Veikoh View Post
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:
  1. Transform your document using a member of the XML family like XSL(T). KW: Conditional import of style sheet.
  2. 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.
Reply With Quote
  #8 (permalink)  
Old 12-06-2007, 10:14 AM
WebProWorld Member
 

Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
Veikoh RepRank 0
Default 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.
Reply With Quote
  #9 (permalink)  
Old 12-06-2007, 11:13 AM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,131
kgun RepRank 3kgun RepRank 3
Default Re: XSLT Conditionals

Quote:
Originally Posted by Veikoh View Post
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
  1. Content (element tags).
  2. Presentation or styling (CSS and / or XSLT).
  3. (Dynamic) behaviour. Programming or scripting.
Reply With Quote
  #10 (permalink)  
Old 12-07-2007, 08:35 AM
WebProWorld Member
 

Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
Veikoh RepRank 0
Default 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?

Reply With Quote
  #11 (permalink)  
Old 12-07-2007, 09:31 AM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,783
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default 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.
Reply With Quote
  #12 (permalink)  
Old 12-07-2007, 09:57 AM
kgun's Avatar
WebProWorld 1,000+ Club
 

Join Date: May 2005
Location: Norway
Posts: 5,131
kgun RepRank 3kgun RepRank 3
Default 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.
Reply With Quote
  #13 (permalink)  
Old 12-10-2007, 09:41 AM
WebProWorld Member
 

Join Date: Jun 2007
Location: Belfast, Northern Ireland
Posts: 47
Veikoh RepRank 0
Default 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.
Reply With Quote
  #14 (permalink)  
Old 04-09-2008, 09:10 AM
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Spain
Posts: 335
computergenius RepRank 1
Default 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.
__________________
Pete Clark
Advertise events locally in Spain for free - http://hotcosta.com/events.php
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
getting XML file to show on separate pages using XSLT? Veikoh Web Programming Discussion Forum 0 09-12-2007 09:01 PM
Office2007, OpenXML and XSLT. kgun IT Discussion Forum 0 06-01-2007 08:57 AM
XSLT Question subho Graphics & Design Discussion Forum 3 01-20-2007 10:36 AM


Search Engine Optimization by vBSEO 3.2.0