Convert lower to upper case for XML element without any attributes

Here should go questions about transforming XML with XSLT and FOP.
sreejitnair
Posts: 1
Joined: Thu Jul 28, 2016 7:16 pm

Convert lower to upper case for XML element without any attributes

Post by sreejitnair » Thu Jul 28, 2016 7:32 pm

Example XML:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book>
<title lang="in">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>

<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>
No I am trying to transform to

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="in">Everyday Italian</title>
<author>GIADA DE LAURENTIIS</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. ROWLING</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title lang="en">XQuery Kick Start</title>
<author>JAMES MCGOVERN</author>
<author>PER BOTHNER</author>
<author>KURT CAGLE</author>
<author>JAMES LINN</author>
<author>VAIDYANATHAN NAGARAJAN</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>ERIK T. RAY</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
so I am trying to convert the value of element without attribute to upper case, so if you see the element "title" is having attribute "lang", so the value for this element should not be converted to upper case.

I got anothe code bit from Oxygen but that is converting all value to upper case. I tried my best but not able to achieve what I want , so can you please help me or advice if at all it is possible?

Code I am using :

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: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>
Thanks
Sreejit

Jamil
Posts: 88
Joined: Thu Oct 23, 2008 6:29 am

Re: Convert lower to upper case for XML element without any attributes

Post by Jamil » Sat Jul 30, 2016 7:07 pm

The reason this is not working is that you have a template match on text(). This will match on all text in your XML input, and it will not be overridden. You will need to do this a different way. Since you have a known set of nodes and attributes, I would suggest you limit your matches to those defined and not be so generic.

Jamil
Posts: 88
Joined: Thu Oct 23, 2008 6:29 am

Re: Convert lower to upper case for XML element without any attributes

Post by Jamil » Sun Jul 31, 2016 2:50 am

I simplified your XSLT quite a bit.

Try this:

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:output method="xml" encoding="UTF-8"/>

<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="node()[not(@lang)]/text()">
<xsl:value-of select="translate(., $smallCase, $upperCase)"/>
</xsl:template>
</xsl:stylesheet>

Post Reply