I have to apply a series of functions to text nodes.
At present, it looks like such:
<xsl:function name="local:enhanceTypo" as="text()">
<xsl:param name="s"/>
<xsl:value-of select="
replace(
replace(
translate($s,'<>','‹›')
,'a.a.O.','a. a. O.'
)
,'z.B.','z. B.')
"/>
</xsl:function>
There is a lot more to be done in local:enhanceTypo and it seems to be funny to make this as a series of nested function calls.
Is there a simple-to-read and -understand way in XSLT2 to sequentially apply a lot of functions to my string?
Certainly chaining variables is one way. I often reuse the same variable name when doing this:
<xsl:variable name="v" select="replace($v, ....)"/>
<xsl:variable name="v" select="replace($v, ....)"/>
<xsl:variable name="v" select="replace($v, ....)"/>
because it's then easy to see what's going on, and to add extra calls into the sequence. I can't speak for other processors, but in Saxon when you do this, the variables are automatically inlined, so it's exactly as if you wrote the deeply nested function call, just easier to read.
If you're into higher-order functions, then with 3.0 there's another way. You can define a sequence of functions like this:
<xsl:variable name="replacements" select="
replace(?, "a", "A"),
replace(?, "b", "B"),
replace(?, "c", "C")"/>
and then you can do a fold-left operation over this sequence:
fold-left($replacements, $string, function ( $in, $f ) { $f($in) })
If your original and replacement strings are in two xs:string* variables $in and $out, then you could instead define the sequence of functions as
<xsl:variable name="replacements" select="
for-each-pair($in, $out, function($i, $o){ replace(?, $i, $o) })"/>
That is, for each pair of input and output strings you create a function that replaces this input by this output, and then you apply the sequence of functions using fold-left.
Fun, eh?
Related
I am using XSLT (2.0) as a java code generator. At the moment I have a XML which describes a database table and I want to generate the entity class for it.
The column names of the table are always lowercase with _ between the words.
Example: bat_valid_from
I want to rename it in the Java class to camelcase with first letter lowercase
Example: batValidFrom
Because I need this quiet often in my codeGen I like to have a function for it.
But I only could achieve this with two sub functions.
<xsl:function name="local:VarName">
<xsl:param name="columnName"/>
<xsl:value-of select="lower-case(substring($columnName,1,1))"/>
<xsl:value-of select="substring(local:VarName_sub($columnName),2)"/>
</xsl:function>
<xsl:function name="local:VarName_sub">
<xsl:param name="columnName"/>
<xsl:value-of select="local:VarName_sub_sub($columnName)"/>
</xsl:function>
<xsl:function name="local:VarName_sub_sub">
<xsl:param name="columnName"/>
<xsl:for-each select="tokenize($columnName, '_')">
<xsl:value-of select="upper-case(substring(.,1,1))"/>
<xsl:value-of select="substring(.,2)"/>
</xsl:for-each>
</xsl:function>
Maybe someone has an idea to simplify this?
Without the sub functions I get the following error:
A sequence of more than one item is not allowed as the first argument of fn:substring()
PS: I haven't posted the whole code to shorten the question
XSLT/XPath 2.0 has supports for expressions. You could do this:
string-join(
for $part in tokenize($input, '_')
return concat(
upper-case(substring($part, 1, 1)),
substring($part, 2)
)
, '')
with $input set to 'bat_valid_from', this expression would produce 'BatValidFrom'.
I'm leaving lower-casing (or not upper-casing) the initial letter as an exercise.
With the hint from Tomalak i was able to make all in one function.
Maybe not light weighted but works like a charm.
<xsl:function name="local:VarName">
<xsl:param name="columnName"/>
<xsl:value-of select="
concat(
lower-case(substring($columnName, 1, 1)),
substring(string-join(for $word in tokenize($columnName, '_')
return concat(
upper-case(substring($word, 1, 1)),
substring($word, 2)), '')
, 2))" />
</xsl:function>
I am new in XSLT and if it is possible to get the position of a specific word? For example, I have a data like this:
<Data>The quick brown fox jumps over the lazy dog!</Data>
I want to get the position of a "brown", "over", "dog" and "!". And, store it in different output name. Like the position of brown is <foo>3</foo>, position of over is <boo>6</boo>, dog <hop>9</hop> and ! <po_df>10</po_df>. Is it possible?
If you were only looking for words you could use tokenize(., '\s+|\p{P}')
<xsl:template match="Data">
<xsl:copy>
<xsl:variable name="words" select="tokenize(., '\s+|\p{P}')"/>
<xsl:for-each select="'brown', 'over', 'dog'">
<matched item="{.}" at-pos="{index-of($words, .)}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
which gives
<Data>
<matched item="brown" at-pos="3"/>
<matched item="over" at-pos="6"/>
<matched item="dog" at-pos="9"/>
</Data>
so it has the right positions (I am not sure where the names of the elements you posted (like hop) are to be taken from so I have not tried to implement that.).
As you also want to identify a punctuation character I am not sure tokenize suffices and even with analyze-string it is not straight-forward to match and collect the position. Maybe someone else has a better idea.
What is the difference between <xsl:variable name="test" select="1"/>
and <xsl:variable name="test" select="'1'"/> ?
if both results are result tre fragments.. so basically the two lines of code above are identical?
If so. how do we decide which to use?
The first sample creates a variable of type number with the number value 1, the second a variable of type string with the string value "1". Result tree fragments are not created with your code samples, that would be done with <xsl:variable name="test">1</xsl:variable>.
As #Martin pointed out, the first one binds the variable to a number and the second one to a string.
how do we decide which to use?
Think of the use you will do with that variable. For example, in the first case you will be able to do:
item[$test]
This won't be possible in the second case, unless you use number() function.
As per the comments below, string or number will not make any difference when using any of the comparison operators. Even when comparing against nodes sets or rtfs. You can read this on the specs (a bit verbose) or try some silly test.
What still is evident is the different behavior you can obtain when dealing with node positions. For example, if you have:
<xsl:variable name="number2" select="2"/>
<xsl:variable name="string2" select="'2'"/>
<xsl:variable name="rtf2">2</xsl:variable>
and you have the input like this:
<root>
<test>a</test>
<test>b</test>
</root>
By using:
<xsl:value-of select="/root/test[$rtf2]"/>
<xsl:value-of select="/root/test[$string2]"/>
<xsl:value-of select="/root/test[$number2]"/>
You will get:
aab
while this:
<xsl:value-of select="/root/test[position()=$rtf2]"/>
<xsl:value-of select="/root/test[position()=$string2]"/>
<xsl:value-of select="/root/test[$number2]"/>
will return:
bbb
due to implicit conversion caused by comparison operators.
XPath 1.0 and XSLT 1.0 treat numbers and strings as pretty much interchangeable, with very few exceptions. A notable exception is item[$test]. But "=" behaves slightly differently too: as numbers 4 and 04 are equal, but as strings they are not.
In XPath 2.0 and XSLT 2.0 the type system is much richer and the difference between strings and numbers is much more noticeable: many operations defined on numbers won't work on strings, and vice-versa.
How to decide? If it's all-numeric, you would normally want to use a number, unless it's something like a phone number, where leading zeros are significant, and it's therefore not really a number but a string of digits.
<block4>
<tag>
<name>50K</name>
<value>/001/002/300060000120135670
CREDIT AGRICOLE ASSET MANAGEMENT</value>
</tag>
</block4>
I need to get output that looks like:
/001/002,/300060000120135670,CREDIT AGRICOLE ASSET MANAGEMENT
I have done like this in XSL, but I didn't get the output I wanted. Can anyone please give me some idea how I could get that output?
<xsl:for-each select ="block4/tag[name = '50K']">
<xsl:value-of select="
concat(
substring(value,1,8),
(concat(substring(value,9,'
'),',')),
substring-after(value,'
')
)
" />,<xsl:text/>
</xsl:for-each>
concat takes any number of arguments, no need to nest those calls. Besides, substring takes a beginning and an optional length, not a terminating character. Try something like this instead:
<xsl:for-each select ="block4/tag[name = '50K']">
<xsl:value-of select="
concat(
substring(value, 1, 8), ',',
substring(substring-before(value,'
'),9), ',',
substring-after(value,'
')
)
" />,<xsl:text/>
</xsl:for-each>
I've kept the final comma in, which is one of the many things you did not really specify.
Why not use XSLT 2.0 tokenize() function?
See Here
I want to check if a value exists in a sequence defined as
<xsl:variable name="some_seq" select="/root/word[#optional='no']/text()"/>
In the past, I've had success with Priscilla Walmsleys function. For clarity, I reproduce it here as follows:
<xsl:function name="functx:is-value-in-sequence" as="xs:boolean">
<xsl:param name="value" as="xs:anyAtomicType?"/>
<xsl:param name="seq" as="xs:anyAtomicType*"/>
<xsl:sequence select="$value=$seq"/>
</xsl:function>
However, this time I need to make a case-insensitive comparison, and so I tried to wrap both $value and $seq with a lower-case(). Obviously, that didn't help much, as $seq is a sequence and lower-case() takes only strings.
Question: what is the best way to either 1) construct a sequence of lower-case strings, or 2) make a case-insensitive comparison analogous to $value=$seq above? TIA!
Question: what is the best way to
either 1) construct a sequence of
lower-case strings
Not many people realize that you can use a function as the last location step in an XPATH 2.0 expression.
You can create a sequence of lower-case() string values with this expression:
/root/word[#optional='no']/text()/lower-case(.)
or 2) make a case-insensitive
comparison analogous to $value=$seq
above?
Using that strategy, you can define a custom function that compares the lower-case() value of the $value and each string value in the $seq:
<xsl:function name="functx:is-value-in-sequence" as="xs:boolean">
<xsl:param name="value" as="xs:anyAtomicType?"/>
<xsl:param name="seq" as="xs:anyAtomicType*"/>
<xsl:sequence select="some $word in $seq/lower-case(.)
satisfies ($word = $value/lower-case(.))"/>
</xsl:function>
Use a "for-expression" inside the function to prepare a lower-case version of the sequence
<xsl:variable name="lcseq" select="for $i in $seq return lower-case($i)"/>
See Michael Kay's "XSLT 2.0 and XPATH 2.0, 4th ed", p. 640
(I haven't tested this)