How to translate to uppercase some text

Here should go questions about transforming XML with XSLT and FOP.
Simona
Posts: 2
Joined: Wed Aug 21, 2013 5:25 pm

How to translate to uppercase some text

Post by Simona »

Hi, I'm an Oxygen user for about a year.
I never posted on this forum before because I've always found all the answers I needed and I am very grateful for this. :)

Now I was trying to translate some part of text from lowercase to uppercase.
It's embarrassing :oops: but I can't figure out how to select just the string I need to translate.

Here an example of the file I'm working on:

Code: Select all


<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lorem ipsum</title>
</head>
<body>
<p>Lorem <small>ipsum</small> dolor sit amet, consectetur adipiscing elit. Integer ullamcorper.</p>
<p>Lorem ipsum dolor sit <small>amet</small>, consectetur adipiscing elit. Integer ullamcorper.</p>
</body>
</html>
I need to translate to uppercase the text tagged as "small", like this:

Code: Select all


 <p>Lorem <small>IPSUM</small> dolor sit amet, consectetur adipiscing elit. Integer ullamcorper.</p>
<p>Lorem ipsum dolor sit <small>AMET</small>, consectetur adipiscing elit. Integer ullamcorper.</p>


And this is the XSLT I was trying to apply:

Code: Select all


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="translate(., $smallCase, $upperCase)"/>
</xsl:template>
</xsl:stylesheet>
This is obviously transforming all the text in uppercase.

How can I make it work only for the text tagged as "small"?
I spent some days trying to understand better XSLT and XPath :roll: but I just can't figure it out.

Thanks a lot!
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: How to translate to uppercase some text

Post by adrian »

Hi,

This stylesheet does what you want, it copies everything and translates to uppercase the text tagged as "small" (including nested elements). This uses two template modes, one for normal copy (copy) and one for the copy with uppercase (uppercase).

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<!-- Match document -->
<xsl:template match="/">
<xsl:apply-templates mode="copy" select="."/>
</xsl:template>
<!-- Deep copy template -->
<xsl:template match="*|text()|@*" mode="copy">
<xsl:copy>
<xsl:apply-templates mode="copy" select="@*"/>
<xsl:apply-templates mode="copy"/>
</xsl:copy>
</xsl:template>

<xsl:template match="xhtml:small" mode="copy">
<xsl:message>123</xsl:message>
<xsl:apply-templates mode="uppercase" select="."/>
</xsl:template>

<!-- Deep copy uppercase -->
<xsl:template match="*|@*" mode="uppercase">
<xsl:copy>
<xsl:apply-templates mode="uppercase" select="@*"/>
<xsl:apply-templates mode="uppercase"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()" mode="uppercase">
<xsl:value-of select="translate(., $smallCase, $upperCase)"/>
</xsl:template>
</xsl:stylesheet>
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Simona
Posts: 2
Joined: Wed Aug 21, 2013 5:25 pm

Re: How to translate to uppercase some text

Post by Simona »

Hi adrian,
this solution is great, you helped me so much!

Thank you! :)
Simona
Post Reply