rumkarma Posted February 1, 2011 Share Posted February 1, 2011 Hi seniors,Please take a look at my XML file : <?xml version="1.0" encoding="ISO-8859-1"?><books><book><title>abcd</title><author>1</author></book><book><title>efgh</title><author>2</author></book><book><title>ijkl</title><author>3</author></book><book><title>mnop</title><author>4</author></book><book><title>qrst</title><author>5</author></book><book><title>uvwx</title><author>6</author></book><book><title>yz</title><author>7</author></book></books> Now I want to display every two titles in a separate <div>. And they must be separated by a delimiter like OR. And if there is less than 2 titles, then it should be in a different <div> too. The output I'm looking for is like this : <html><head><title>new</title></head><body><div>#abcdOR#efgh</div><div>#ijklOR#mnop</div><div>#qrstOR#uvwx</div><div>#yz</div></body></html> I tried using a global variable but it takes only the local value and I keep getting as many divs as there are titles.Also i get trailing delimiters which I'm looking to avoid. Please help me out here. Link to comment Share on other sites More sharing options...
Martin Honnen Posted February 1, 2011 Share Posted February 1, 2011 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="n" select="2"/> <xsl:template match="/"><html><head><title>new</title></head><body><xsl:apply-templates select="books/book[position() mod $n = 1]" mode="group"/></body></html></xsl:template><xsl:template match="book" mode="group"> <div> <xsl:apply-templates select="./title | following-sibling::book[position() < $n]/title"/> </div></xsl:template><xsl:template match="book/title"> <xsl:if test="position() > 1">OR</xsl:if> <xsl:text>#</xsl:text> <xsl:value-of select="."/></xsl:template></xsl:stylesheet> Link to comment Share on other sites More sharing options...
rumkarma Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks a lot Martin. Is there a way in which I can create a variable which contain the value of every pair (which is to be passed to a cpp function) instead of displaying within <div> ? (I have to do this for every 50 entries actually) Would wrapping the code in <xsl:variable> tags be enough? Link to comment Share on other sites More sharing options...
rumkarma Posted February 2, 2011 Author Share Posted February 2, 2011 I'm getting the following error message when i use the above code:'The character '<' cannot be used in an attribute value.' Referring to the code segment:<xsl:apply-templates select="./title | following-sibling::book[position() < $n]/title"/>What is wrong ?? can anybody help me out?? Link to comment Share on other sites More sharing options...
Martin Honnen Posted February 2, 2011 Share Posted February 2, 2011 I think the code I posted is fine, I even run it before posting. And even the snippet you cite, the <xsl:apply-templates select="./title | following-sibling::book[position() < $n]/title"/> does not contain the character '<' literally, rather it is properly escaped '<', as XML syntax rules require. Thus I am not sure why you get that error. How exactly do you test/run the code? It sounds as if you alter it and have created something that does not properly escape the character '<' as '<'.As for Is there a way in which I can create a variable which contain the value of every pair (which is to be passed to a cpp function) instead of displaying within <div>I am not sure what a cpp function is. Is that a C++ function you want to call from inside of the XSLT stylesheet? I am not sure whether any XSLT processor supports that and if there is one then how you do that and which types you can pass on is highly implementation dependent. Link to comment Share on other sites More sharing options...
rumkarma Posted February 2, 2011 Author Share Posted February 2, 2011 My bad. It works perfectly. Thanks a lot Martin. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.