hi All, I'm facing prob using <xsl:choose> in my stylesheet while working with Cocoon. When I use the conditional statement - "<xsl:choose> <xsl:when test=".[tag1='FirstTag']"> ..some code </xsl:when> <xsl therwise> ..some code </xsl therwise> </xsl:choose> " The Cocoon engine throws following exception - " org.apache.xalan.xslt.XSLProcessorException: pattern = '.[tag1='FirstTag']' '..[predicate]' or '.[predicate]' is illegal syntax. Use 'self::node()[predicate]' instead. "
And when I use " self::tag1['FirstTag'] ".The engine works fine without executing the code of <xsl:choose> tag. Can anyone please help me out.What I feel is there may be some syntax problem but I failed to locate it. Thanks in advance. Regards, SHAILENDRA
Shailendra. "." stands for the current node. What is your current node and what are relationships between it and tag1? If you could post an exempt from your input XML, we would probably help you better...
hi Mapraputa, Here is my code--- MyXML.xml --------- <?xml version="1.0"?> <?cocoon-process type="xsp"?> <?cocoon-process type="xslt"?> <?xml-stylesheet href="MyXSL.xsl" type="text/xsl"?> <xsp age language="java" xmlns:xsp="http://www.apache.org/1999/XSP/Core"> <page> <title>Hello</title> <tag1>FirstTag</tag1> <tag2>SecondTag</tag2> <p>Hello World!.</p> </page> </xsp age> -------------------------------------------------------------- Now in my xsl I want to check the value of <tag1>,if it's "FirstTag" then do something otherwise do something else.Hope this will help to find out the prob My <xsl:choose>[as posted above] is not working.
Thanks. Regards, SHAILENDRA
Leverager of our synergies
posted 17 years ago
There are two approaches in getting information out of XML document: "pull" and "push". <xsl:choose> element belongs to "pull" approach: we locate our information of interest, read it and conceivably output something. The whole XSLT in your case will be: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:choose> <xsl:when test="//tag1='FirstTag'"> tag1 has content 'FirstTag' </xsl:when> <xsl:otherwise> tag1 has another content </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Another approach would be to use <xsl:template> element and code your condition in its "match" attribute: <xsl:template match="tag1[.='FirstTag']"> The whole XSLT is: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="tag1[.='FirstTag']"> tag1 has content 'FirstTag' </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>
In this case you do not "pull" anything out of XML document. XSLT processor walk through all elements and applies templates. Every time, tag1 has content of 'FirstTag', your template will be applied and "tag1 has content 'FirstTag'" will be in the output. If you had two tag1, two such messages would be outputted. In contrast, with the first approach, you could see only one message. (Actually, as many as enclosing template would be called, in this case template's condition is "/" which match root element, and there always only one root, so the template will be called once). With "push" approach (template/match) your logic is placed inside a template and is called when XSLT processor "stays" on the element specified in a match condition. So elements of interest are dealt with in a "natural" order, the order they are in the XML file, and your template will be applied as many times as there are such elements. With "pull" approach (<xsl:choose>, <xsl:if>, <xsl:value>...), you can check your condition from any place, you do not wait for XSLT processor reach your element of interest. Also with this approach you can extract target information as many times as you want - either once, or check the condition repeatedly and output your reaction several times.
[This message has been edited by Mapraputa Is (edited August 03, 2001).]
hi Mapraputa Is, Thanks for such a detailed reply.Actually I was out for a few days so couldn't reply you immediately.My prob has been solved.But there is one more thing troubling me .In this case,I'm generating .xml file using DOM Object reading contents from my database.Now I want to insert a tag <xsp : page> in my .xml file when it gets created.So when I use " set rootElement=domObj.CreateNode("Element","xsp : page","") domObj.appendchild(rootElement) " The server throws error. I even tried doing it using ISO-Latin-1 Character Set[:] but couldn't succeed.Can you help me out. Thanks in advance. Regards, SHAILENDRA.
[This message has been edited by shailendra vasale (edited August 06, 2001).]