views:

26

answers:

1

can I change the first letter of the second sentence to lowercase using the XSL template? Or is there a way to change the first letter of the second sentence to lower case in HTML (first-letter pseudo code DOES NOT work since it can only be the subject of the selector).

i.e:

In XLM: "Name", "*R*eturns the something of the something."

Need (in HTML): Name *r*eturns the something of the something.

+1  A: 

This XSLT transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:variable name="v1stchar" select=
  "substring(translate(s[2],translate(s[2],'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ''),''),1,1)"/>
  <xsl:value-of select=
  "concat(s[1],
          ' ',
          substring-before(s[2], $v1stchar),
          translate($v1stchar,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
          substring-after(s[2], $v1stchar)
          )
  "/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>
 <s>Name</s>
 <s>*R*eturns the something of the something.</s>
</t>

produces the wanted, correct result:

Name *r*eturns the something of the something.

It is possible to do this even in a single XPath expression, but it would look really unwieldy.

Dimitre Novatchev
@Dimitre: Good answer, but I think the OP's question has those `*` just for clarification. Also, it must say that with `fn:translate()` you need to explicitly define the alfabeth. You know, I'm not english.
Alejandro
@Alejandro: No problem, just pass the alphabet, lower and upper-case as parameters.
Dimitre Novatchev
@Alejandro: It is useless to argue what the OP may have been thinking... We have what he wrote. Obviously, by XLM he means XML, and by "sentense" he means "string". Everything else is a guess and let's not compare my phantasy to yours :)
Dimitre Novatchev
@Dimitre: +1 For phantasy race! About alphabet: I don't remember now but aren't there alphabets with two character lowercase letter that become one character uppercase letter?
Alejandro
@Alejandro, @Dimitre: I think the only sane way to cure that is to use XPath 2.0's fn:lower-case(): "Returns the value of $arg after translating every character to its lower-case correspondent as defined in the appropriate case mappings section in the Unicode standard [The Unicode Standard]."
LarsH
@Dimitre, judging by the name, the OP may even be a she. :-)
LarsH
@Alejandro, @LarsH: Sure, if the language of the OP is an exotic one (and judging by the name she may be Greek) then we'd need XSLT 2.0 and functions like `normalize-unicode()`. But I think even for Greek, passing in two params the capitals and lowercase leters would work -- as in Bulgarian :)
Dimitre Novatchev