XSLT: Define a param with & ampersand - string

I need to define a xsl:param with a string that contains & character for further processing.
For example:
<xsl:param name="greeting" as="xs:string">Hi & Bye</xsl:param>
Important Notice:
I am using a xslt converter component in a webservice tool. What I do, is that I just initialize the param and as the webservice is called the param is set to a value of a variable which was set in previous steps. So I have no control on the string, i.e meaning I can't use &
Any ideas how to do that?
Thanks in advance.

Encode it.
& is encoded as &:
<xsl:param name="greeting" as="xs:string">Hi & Bye</xsl:param>
See this document about XML Character entities.
Another option is to enclose such strings in CDATA sections:
<xsl:param name="greeting" as="xs:string"><![CDATA[Hi & Bye]]></xsl:param>

You could attempt to store the string as unparsed character data (CDATA). Marking the string in that case will notify an XML parser that the contained information should not be parsed:
<xsl:param name="greeting" as="xs:string"><![CDATA[ Hi & Bye ]]></xsl:param>

Related

How do I address an item in a sequence when using tokenize()

I have an HTML that contains a style attribute with a list of values separated by semicolons:
<td style="border-width:1pt;border-color:#FFFFFF;border-style:solid">
I want to split this list:
<xsl:variable name="astyle"><xsl:value-of select="tokenize(#style, ';')"/></xsl:variable>
This gives me a sequence. Now I want to select the first item in the sequence.
<xsl:value-of select="$astyle[1]"/>
result:
"border-width:1pt border-color:#FFFFFF border-style:solid"
so that's the entire sequence instead of the first item in the sequence.
I can't find any documentation on this.
Edit: it seems my assumption is incorrect. Tokenize() contains a sequence of 1 item. I was expecting 3 items.
How can I split a string into items that can be addressed individually?
I want to do something like
<xsl:value-of select="$astyle[1]"/>
and get:
border-width:1pt
Use <xsl:variable select="tokenize(...)" name="astyle"/>, then the value of the variable is a sequence and $astyle[1] works.

Merge string differences with Access SQL/VBA

I have two Fields, with partly different strings. FieldA:= "String1" FieldB:= "String1; String2" (So, the main difference between the two fields is the "; String2" in FieldB). The result i want to see is also "String1; String2", but the first half i want from FieldA, and the second half i want from FieldB. Is there any way using Access SQL/VBA function to solve this problem?
With the assumption that your values will always contain a semi-colon, you could also use the Split function in the following way:
[FieldA] & ";" & Split([FieldB],";")(1)
Yes. Use string manipulation functions. This is a relatively simple case for string manipulation, assuming the strings are consistent with the examples given. Consistency is critical to string manipulation. Assuming there is a space following the semi-colon, try:
[FieldA] & "; " & Mid([FieldB], InStr([FieldB], ";") + 2)
Expression can be used in query or textbox or VBA.
Suggest you do some research and learn about these and other string functions.

Append literal string (plain text) to XPath result

Is there a way to append a literal string to whatever an XPath expression gets you?
e.g. from following XML:
<root>
<select>I am</select>
</root>
I would like to produce:
I am a literal
purely with XPath. What do I add to /root/select to get what I want? Important: XPath 1.0 solution required! I'm not using XSLT.
Any reason you can't simply concat() 'a literal' to the end?
Something like: concat( string(/some/selector) , ' some literal' )?

Sharepoint XSLT Dynamic filtering

I'm trying to create a dynamic row filter based on a variable. I have the following code:
<xsl:variable name="filter" select="contain(#Title, 'title1') or contain(#Title, 'title2')"/>
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[string($filter)]" />
This unfortunately doesn't seem to work and I end up with all rows. I'm guessing the filter doesn't actually get applied, since I can copy and paste the output of the $filter variable, copy and paste it in the Row[] and it works as expected.
Anyone tried to do this before?
In case you're wondering the filter variable is actually created using a template that splits a string like:
title1 - title2 - title3
and returns a string like:
contain(#Title, 'title1') or contain(#Title, 'title2') or contain(#Title, 'title3')
Any help would be greatly appreciated!
You can't do what you seem to be attempting here. An XPath expression is atomical, you can't save parts of it and re-use them (apart from that it is contains(), not contain()).
You need something like this:
<xsl:variable name="Rows" select="
/dsQueryResponse/Rows/Row[
contains(#Title, 'title1') or contains(#Title, 'title2')
]
" />
Your "filter" does not work because if $filter is a string, then it is a string, nothing else. It does not get a magical meaning just because it looks like XPath. ;-)
This
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[string($filter)]" />
evaluates to a non-empty string as the predicate. And any non-empty string evaluates to true, which makes the expression return every node there is.
If you want a dynamic filter based on an input string, then do this:
<xsl:variable name="filter" select="'|title1|title2|title3|'" />
<xsl:variable name="Rows" select="
/dsQueryResponse/Rows/Row[
contains(
$filter,
concat('|', #Title, '|')
)
]
" />
The use of delimiters also prevents "title11" from showing up if you look for "title1".
Make sure your filter always starts and ends with a delimiter, and use a delimiter that is reasonably unlikely to ever occur as a natural part of #Title. (For example, you could use 
. If your title cannot be multi-line this is pretty safe.)

XSLT conversion of string representation of date (01 Jan 00) into xs:date

I'm stuck with a string conversion to xs:date. I would really appreciate any help and tips!
I have a string that represents date in the format of "01 Jan 00", which I need to convert to xs:date, so that I can manipulate it further.
Is there a function or something already there so that I can convert my so ever difficult string representation of date? Would I need to write a function from scratch to convert month in "MN" format into its number representation?
Please help! :)
Thank you!
Daria
The EXSLT library might be a good option: http://www.exslt.org/
i think you'll need a function for this one. something similar is described here
You could bind an extension function to convert the date string to the desired format. The exact mechanism will depend on your XSLT engine.
In .NET, we can take the following method:
public class DateConv
{
public string AsXsDate( string date )
{
return DateTime.Parse(date).ToString("yyyy-MM-dd");
}
}
XsltArgumentList args = new XsltArgumentList();
args.AddExtensionObject("urn:date-conv", new DateConv());
xslt.Transform( input, args output );
and use it in a stylesheet like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="urn:date-conv">
<xsl:template match="bookstore">
<xsl:value-of select="date:AsXsDate('01 Jan 00')"/>
</xsl:template>
</xsl:stylesheet>
I ended up writing a simple function that would determine between two date formats I have in my XML: "01 Jan 00" and a standard date-time string "2000-01-01"; and convert the non-standard to standard "YYYY-MM-DD".
Then I use EXSLT functions (http://www.exslt.org/date/index.html) to manipulate with dates further.

Resources