Is it possible to flip and rotate text using XSLT?

I would like to take the contents of an XML tag and display it both horizontally (mirror image) and vertically (as a column) when viewing through a stylesheet. Is this possible without using random third-party libraries?

<mytag>Random Data</mytag> 
+4
source share
4 answers

XSLT is for conversion. To change the presentation, you must use CSS or XSL-FO.

In XSL-FO you can set writing-mode to tb-lr

+3
source

Therefore, XSLT is not suitable for string processing. With XSLT 2.0, things get better as more string functions are available and sequence-based operations are possible.

In XSLT 1.0 (which is still the most portable version for writing code), character-by-character processing can only be achieved by recursion. For pleasure it is:

 <xsl:output method="text" /> <xsl:variable name="CRLF" select="'&#13;&#10;'" /> <xsl:template match="/mytag"> <!-- flip string --> <xsl:call-template name="reverse-string"> <xsl:with-param name="s" select="string(.)" /> </xsl:call-template> <xsl:value-of select="$CRLF" /> <!-- vertical string --> <xsl:call-template name="vertical-string"> <xsl:with-param name="s" select="string(.)" /> </xsl:call-template> </xsl:template> <xsl:template name="reverse-string"> <xsl:param name="s" select="''" /> <xsl:variable name="l" select="string-length($s)" /> <xsl:value-of select="substring($s, $l, 1)" /> <xsl:if test="$l &gt; 0"> <xsl:call-template name="reverse-string"> <xsl:with-param name="s" select="substring($s, 1, $l - 1)" /> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template name="vertical-string"> <xsl:param name="s" select="''" /> <xsl:variable name="l" select="string-length($s)" /> <xsl:value-of select="concat(substring($s, 1, 1), $CRLF)" /> <xsl:if test="$l &gt; 0"> <xsl:call-template name="vertical-string"> <xsl:with-param name="s" select="substring($s, 2, $l)" /> </xsl:call-template> </xsl:if> </xsl:template> 

It produces:

 ataD modnaR R a n d o m D a t a 

EDIT: To be clear: I do not support the actual use of the above code. Presentation issues must be addressed at the presentation level. The above will work, but char -by-char recursion is one of the most inefficient ways to do string processing, and if you have no other choice, avoid string handling in XSLT.

+4
source

Vartek is right. I ended it with the following CSS:

 .verticaltext { writing-mode: tb-rl; filter: flipv; } 

Thanks!

+2
source

This is very easy to do with XPath 2.0 / XSLT 2.0 (you can use FXSL 1.x functions / templates in XSLT 1.0 ):

Both tasks can be created as a result of XPath 2.0 expressions:

1. Cancel the line:

  codepoints-to-string( reverse(string-to-codepoints($vText)) ) 

2. Verticalize the line:

  replace($vText, '(.)', '$1&#xA;') 

where the line we want to use is contained in the $vText variable.

To see this in action, we simply put the XPath expressions above into the XSLT 2.0 stylesheet :

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:variable name="vText" as="xs:string" select="'Random Data'"/> <xsl:variable name="vReversed" as="xs:string" select="codepoints-to-string( reverse(string-to-codepoints($vText)) ) " /> <xsl:variable name="vVertical" as="xs:string*" select="replace($vText, '(.)', '$1&#xA;')"/> <xsl:template match="/"> <xsl:sequence select="$vReversed"/> =================================== <xsl:text/> <xsl:sequence select="$vVertical"/> </xsl:template> </xsl:stylesheet> 

When this conversion is performed (no matter on what (if any) XML document), the desired result is created : :)

 ataD modnaR =================================== R a n d o m D a t a 

Note the use of the following standard XPath 2.0 features:

+1
source

All Articles