Subject: Re: how to put or test for quotes in a string From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Sun, 13 Aug 2000 10:43:53 +0100 |
[Resent from recognised email address] Mike, Good question! XML defines entities for ' and " (' and ", somewhat unsurprisingly). In certain situations, it is possible to use these. Your first example, for instance, could also be given as: <xsl:variable name="foo" select="'"Hello, world!"'"/> When this is parsed by the XML parser, the value of the 'select' attribute is set to (no extra quotes included): '"Hello, world!"' When the XSLT Processor sees this, it recognises the external quotes as designating a string value, and so sets the variable $foo to the string (no extra quotes included): "Hello, world!" The thing to remember is that you are escaping the " and ' *for the XML parser* and not for the XSLT processor. So your second example: >How do you test for the presence of ' or " in a string? > ><!-- same $apos and $quot assignments, then... --> ><xsl:if test="contains($foo,$apos) or contains($foo,$quot)"> > ... ></xsl:if> can be escaped as: <xsl:if test="contains($foo, "'") or contains($foo '"')"> ... </xsl:if> As there are no unescaped "s within the attribute value, the XML parser can parse this and emerges with the value of the 'test' attribute as: contains($foo, "'") or contains($foo, '"') The XSLT processor can again recognise that "'" designates a string with the value of the single character ' and that '"' designates a string with the value of the single character ". Similarly, if you wanted single quotes rather than double quotes around your Hello, World!, then you should do: <xsl:variable name="foo" select=""'Hello, world!'"" /> (-> "'Hello, world!'" att. value -> 'Hello, world!' string) [Or, alternatively: <xsl:variable name="foo" select='"'Hello, world!'"' /> (-> "'Hello, world!'" att. value -> 'Hello, world!' string)] So, for fairly simple situations like this, it is enough to use the normal XML escaping to get the XSLT processor to see something that it can understand. However, difficulties arise when the quote nesting goes deeper than this. For example, if you wanted to see whether a string contains the string (no extra quotes): "You're here" There is no way to wrap quotes around that string, and no way that I know of within XSLT/XPath to escape internal quotes like this (the XSLT processor is not an XML parser - it won't detect and recognise "/' itself). In these cases, your method, using variables, is the only solution. (BTW, I'd personally declare the variables as: <xsl:variable name="apos" select='"'"' /> <xsl:variable name="quot" select="'"'" /> so that they are set as strings rather than result tree fragments.) I hope that helps, Jeni Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
how to put or test for quotes in a , Mike Brown | Thread | Saxon and jvm, Dave Pawson |
Re: Antwort: comments. (Re: key() R, Jeni Tennison | Date | Re: relational operators, Jeni Tennison |
Month |