XSLT check node availability in - xslt

I am using XSLT 1.0 in my project. In my XSLT transformation I have to check for a specific element and if that exists - I have to perform some concatenation or else some other concatenation operation.
However, I am not finding an option here, like some built-in function.
Requirement is like
<Root>
<a></a>
<b></b>
<c></c>
</Root>
Here is element <a>, come in request payload, then we need to perform concatenation of <b> and <c> else <c> and <b>.

You can do that with template matching:
<xsl:template match="Root[not(a)]">
<xsl:value-of select="concat(c, b)"/>
</xsl:template>
<xsl:template match="Root[a]">
<xsl:value-of select="concat(b, c)"/>
</xsl:template>

Try it along these lines:
<xsl:template match="/Root">
<xsl:choose>
<xsl:when test="a">
<!-- do something -->
</xsl:when>
<xsl:otherwise>
<!-- do something else -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Explanation: the test returns the Boolean value of the node-set selected by the expression a. If the node-set is non-empty, the result is true.

Test for the presence of the element, using xsl:choose
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/Root">
<xsl:choose>
<xsl:when test="a">
<xsl:value-of select="concat(c, b)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(b, c)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Or in a predicate for template matches:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/Root[a]">
<xsl:value-of select="concat(c, b)"/>
</xsl:template>
<xsl:template match="/Root[not(a)]">
<xsl:value-of select="concat(b, c)"/>
</xsl:template>
</xsl:stylesheet>

In your case, use choose and test for the presence of a using boolean() on the according xpath.
<xsl:template match="Root">
<xsl:choose>
<xsl:when test="boolean(./a)">
<xsl:value-of select="concat(./b, ./c)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(./c, ./b)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Related

XSLT - Can't get data from node, when using xml:when

I have a problem getting data from a node, when I'm using xml:choose and xml:when. I only get the result NaN or the value from the main xml-file, even if.
Part of the XML-file:
<?xml version="1.0" encoding="UTF-8"?>
<Job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Invoice>
<InvoiceLine>
<LineNo>1</LineNo>
<QtyInSecondUnit>56</QtyInSecondUnit>
<Quantity>56</Quantity>
<CustTaric>
<StatNo>34011100</StatNo>
<IssuingCountry>GB</IssuingCountry>
</CustTaric>
</InvoiceLine>
<InvoiceLine>
<LineNo>2</LineNo>
<QtyInSecondUnit>22</QtyInSecondUnit>
<Quantity>0</Quantity>
<CustTaric>
<StatNo>44152020</StatNo>
<IssuingCountry>GB</IssuingCountry>
</CustTaric>
</InvoiceLine>
</Invoice>
</Job>
Part of the XSLT-file:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="Job/Invoice"/>
</xsl:template>
<xsl:template match="Job/Invoice/InvoiceLine">
<xsl:apply-templates select="QtyInSecondUnit"/>
<xsl:apply-templates select="Quantity"/>
<xsl:apply-templates select="CustTaric/StatNo"/>
</xsl:template>
<xsl:template match="QtyInSecondUnit">
<xsl:choose>
<xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Quantity">
<xsl:choose>
<xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
<xsl:value-of select="'0'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="CustTaric/StatNo">
<xsl:value-of select="."/>
</xsl:template>
Hope there is somebody how can tell me (the noob) what I'm doing wrong here?
Here's the line producing NaN
<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
There are two problems (one of which is probably where you have over-simplified your XML)
The xpath expression you are using will be relative to the current node you are positioned on. There is no Job element under the current QtyInSecondUnit element
There is no NetMass element in your XML in your question
Assuming NetMass does exist in your actual XML, and is a child of the parent InvoiceLine the expression you want is this
<xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>
There is also an issue with your xsl:when (possibly)
<xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
This will test for any CustTaric/StatNo anywhere in the document. Perhaps you only want to test for the one in the current InvoiceLine? If so, do this...
<xsl:when test="../CustTaric/StatNo = '44152020'">
Note, you could rewrite your XSLT to put the logic in template matches, rather than xsl:choose
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Job/Invoice/InvoiceLine">
<xsl:apply-templates select="QtyInSecondUnit"/>
<xsl:apply-templates select="Quantity"/>
<xsl:apply-templates select="CustTaric/StatNo"/>
</xsl:template>
<xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/QtyInSecondUnit">
<xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>
</xsl:template>
<xsl:template match="QtyInSecondUnit">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/Quantity">
<xsl:value-of select="'0'"/>
</xsl:template>
<xsl:template match="Quantity">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="CustTaric/StatNo">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Strictly speaking, the templates that just do <xsl:value-of select="."/> can be removed, as XSLT's built-in templates will do exactly the same thing if there is no matching template in the XSLT.

How to find word with hyphen?

Is it possible to find words separated by a hyphen and surround them with some tag?
input
<root>
text text text-with-hyphen text text
</root>
required output
<outroot>
text text <sometag>text-with-hyphen</sometag> text text
</outroot>
This XSLT 2.0 transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root/text()">
<xsl:analyze-string select="." regex="([^ ]*\-[^ ]*)+">
<xsl:matching-substring>
<sometag><xsl:value-of select="."/></sometag>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<root>
text text text-with-hyphen text text
</root>
produces the wanted, correct result:
<root>
text text <sometag>text-with-hyphen</sometag> text text
</root>
Explanation:
Proper use of the XSLT 2.0 <xsl:analyze-string> instruction and its allowed children-instructions.
Just checked, it works. So idea behind is create recursive iteration over entire text. And inside recursion step using XPath function contains detect if word (see usage of $word) contains hyphen:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<outroot>
<xsl:call-template name="split-by-space">
<xsl:with-param name="str" select="text()"/>
</xsl:call-template>
</outroot>
</xsl:template>
<xsl:template name="split-by-space"> <!-- mode allows distinguish another tag 'step'-->
<xsl:param name="str"/>
<xsl:if test="string-length($str)"><!-- declare condition of recursion exit-->
<xsl:variable name="word"> <!-- select next word -->
<xsl:value-of select="substring-before($str, ' ')"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($word, '-')"> <!-- when word contains hyphen -->
<sometag>
<xsl:value-of select='concat(" ", $word)'/><!-- need add space-->
</sometag>
</xsl:when>
<xsl:otherwise>
<!-- produce normal output -->
<xsl:value-of select='concat(" ", $word)'/><!-- need add space-->
</xsl:otherwise>
</xsl:choose>
<!-- enter to recursion to proceed rest of str-->
<xsl:call-template name="split-by-space">
<xsl:with-param name="str"><xsl:value-of select="substring-after($str, ' ')"/></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Insert XSLT node-set B in another node-set A at detected position inside text node of node-set A

I'm using XSLT to transform complex XML output of a content management system into XHTML. I'm using <xsl:apply-templates/> to get an XHTML fragment of whatever is described by XML input. That XML input comes with a very complex structure that may describe lots of different cases to be handled by several XSLT template elements. And that structure may change quite often in the future.
Previously, the resulting fragment of that transformation was directly sent to XSLT output. Now the requirements have changed and I need to capture result, occasionally modify it to insert some other well-formed XHTML fragment at a certain position in value of the fragment.
For the sake of demonstration, consider <xsl:apply-templates/> having created some opaque XHTML fragment captured in variable container.
<xsl:variable name="container">
<xsl:apply-templates />
</xsl:variable>
Next there is a second XHTML fragment in variable snippet:
<xsl:variable name="snippet">
<xsl:call-template name="get-snippet" />
</xsl:variable>
Requirement says to have node-set in $snippet to be inserted before any optionally contained period at end of value of $container. This ain't problematic unless XHTML fragments in both variables have to be kept as fragments. Thus one can't operate on string values of either variable.
Is there any opportunity to achieve that requirement in XSLT without losing the power and flexibility of <xsl:apply-templates/> on retrieving XHTML fragment in $container?
BTW: I already know about accessing the last text node in $container using:
node-set($container)//child::text[last()]
But I missed to get something inserted in the mid of that text node and I consider XSLT failing to provide proper support for what I want to do.
I. XSLT 1.0 solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vrtfContainer">
<html>
<p>Hello, world.</p>
<p> This is <b>just</b> a <i>demo.</i></p>
<p> Of some text</p>
</html>
</xsl:variable>
<xsl:variable name="vContainer" select=
"ext:node-set($vrtfContainer)/*"/>
<xsl:variable name="vrtfSnippet">
<p>Snippet</p>
</xsl:variable>
<xsl:variable name="vSnippet" select=
"ext:node-set($vrtfSnippet)/*"/>
<xsl:variable name="vText" select=
"($vContainer//text()[contains(.,'.')])[last()]"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$vContainer"/>
</xsl:template>
<xsl:template match="text()">
<xsl:choose>
<xsl:when test="not(generate-id() = generate-id($vText))">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="insertSnippet">
<xsl:with-param name="pText" select="$vText"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="insertSnippet">
<xsl:param name="pText"/>
<xsl:copy-of select="substring-before($pText, '.')"/>
<xsl:variable name="vTail" select=
"substring-after($pText, '.')"/>
<xsl:choose>
<xsl:when test="not(contains(substring($vTail,2), '.'))">
<xsl:copy-of select="$vSnippet"/>
<xsl:value-of select="concat('.', $vTail)"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>.</xsl:text>
<xsl:call-template name="insertSnippet">
<xsl:with-param name="pText"
select="substring-after($pText, '.')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied (to any XML document -- not used), the wanted, correct result is produced:
<html>
<p>Hello, world.</p>
<p> This is <b>just</b> a <i>demo
<p>Snippet</p>.</i></p>
<p> Of some text</p>
</html>
Explanation: Identity rule overriden by a recursive named template to find the last '.' in a string.
II. XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vContainer">
<html>
<p>Hello, world.</p>
<p> This is <b>just</b> a <i>demo.</i></p>
<p> Of some text</p>
</html>
</xsl:variable>
<xsl:variable name="vSnippet">
<p>Snippet</p>
</xsl:variable>
<xsl:variable name="vText" select=
"($vContainer//text()[contains(.,'.')])[last()]"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$vContainer/*"/>
</xsl:template>
<xsl:template match="text()[. is $vText]">
<xsl:variable name="vInd" select=
"index-of(string-to-codepoints(.), string-to-codepoints('.'))[last()]"/>
<xsl:sequence select="substring(., 1, $vInd -1)"/>
<xsl:sequence select="$vSnippet/*"/>
<xsl:sequence select="substring(., $vInd)"></xsl:sequence>
</xsl:template>
</xsl:stylesheet>
When this XSLT 2.0 transformation is performed, again the wanted correct result is produced:
<html>
<p>Hello, world.</p>
<p> This is <b>just</b> a <i>demo
<p>Snippet</p>.</i></p>
<p> Of some text</p>
</html>
Explanation: Use of the standard XPath 2.0 functions string-to-codepoints(), index-of(), substring() and operator is.

How to force errors on a <xsl:param> without "select" attribute?

If I use <xsl:param> without specifying a value, the transformer assumes that the value is an empty string.
In other words, if I forgot to specify a value (e.g. <xsl:param name="N"/>), the compiler doesn't signal an error. This may cause my program to fail silently, which is a bad thing.
How can I specify that my <xsl:param> must have an explicit value? For example, this code should give me an error because there is no explicit value specified:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="F1"></xsl:call-template>
<html>
<head>
<title></title>
</head>
<body>stuff</body>
</html>
</xsl:template>
<xsl:template name="F1">
<xsl:param name="N"/> <!-- I Should Get An Error Here! -->
</xsl:template>
</xsl:stylesheet>
Am looking for a solution in both XSLT 1.0 and XSLT 2.0.
In XSLT 2.0, of course, you can say <xsl:param required="yes">, so the problem goes away.
You could actually do this with a bit of meta-XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="xsl:call-template">
<xsl:variable name="template" select="/xsl:stylesheet/xsl:template[#name=current()/#name]"/>
<xsl:variable name="call" select="." />
<xsl:variable name="desc">
<xsl:value-of select="concat('call to named template "',$template/#name,'" in ')"/>
<xsl:choose>
<xsl:when test="ancestor::xsl:template/#name">
<xsl:value-of select="concat('named template "',ancestor::xsl:template/#name,'"')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('template matching "',ancestor::xsl:template/#match,'"')" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:for-each select="$template/xsl:param[not(#select)]">
<xsl:if test="not($call/xsl:with-param[#name=current()/#name])">
<xsl:value-of select="concat('Missing parameter "',#name,'" in ',$desc)" />
</xsl:if>
</xsl:for-each>
<xsl:for-each select="xsl:with-param">
<xsl:if test="not($template/xsl:with-param[#name=current()/#name])">
<xsl:value-of select="concat('Unrecognised parameter "',#name,'" in ',$desc)" />
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
This stylesheet takes any stylesheet as an input, and checks that all call-template's have the right parameters, outputting a message if there's any errors.
This obviously isn't going to put the error checking in the transformer itself, but it will list ALL errors in one go, and can potentially be extended to check for other issues as well.
EDIT: I've adapted it to handle optional parameters, and added in a means of describing where the error is; it's actually a bit of a redesign, with optional parameters simply counting them was going to be tricky, so I removed that bit. Every error is itemized anyway, so the count wasn't really necessary.
<xsl:param name="foo" select="false" />
<xsl:if test="not($foo)">
<xsl:message terminate="yes">You called me with improper params</xsl:message>
</xsl:if>
A simple way is checking the input parameter to be not an empty string (specific case mentioned in your comment):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="test">
<xsl:param name="nodefault"/>
<xsl:choose>
<xsl:when test="boolean($nodefault)">
<xsl:message>do your stuff</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">Your stuff can't be done</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="test"/>
</xsl:template>
</xsl:stylesheet>
or much simpler:
<xsl:template name="test">
<xsl:param name="nodefault"/>
<xsl:if test="not($nodefault)">
<xsl:message terminate="yes">Your stuff can't be done</xsl:message>
</xsl:if>
<!-- do your stuff -->
</xsl:template>
There's a similar option, where you can use a variable that make a choose for the parameter in the called template, for example:
<xsl:template match="/">
<!-- call 1 -->
<xsl:apply-templates select="//encabezado/usuario" mode="forma1">
<xsl:with-param name="nombre" select="'wwww1'"/>
</xsl:apply-templates>
<!-- call 2 -->
<xsl:apply-templates select="//encabezado/usuario" mode="forma1">
</xsl:apply-templates>
<xsl:template match="node()" mode="forma1">
<xsl:param name="nombre"/>
<xsl:param name="valor"/>
<xsl:variable name="nombreLocal">
<xsl:choose>
<xsl:when test="normalize-space($nombre)">
<xsl:value-of select="$nombre"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="local-name()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select ="$nombreLocal"/>
</xsl:template>

Using max in a variable in XSLT 1.0

can I use max function in a variable in XSLT 1?
I need to find a maximum value inside some nodes and I'll need to call this from more places.
So I tried to create a template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:essox="urn:essox-scripts">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="Field001_max_dluznych_splatek">
<xsl:param name="CrRep"/>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:value-of
select="max($CrRep
/Response
/ContractData
/Installments
/InstDetail
/NrOfDueInstalments)" />
</xsl:variable>
<xsl:variable name="PocetDluznychSplatekKarty">
<xsl:value-of
select="max($CrRep
/Response
/ContractData
/Cards
/CardDetail
/NrOfDueInstalments)" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$PocetDluznychSplatekSplatky
>= $PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
In XML Spy I get this error:
Error in XPath expression Unknown
function - Name and number of
arguments do not match any function
signature in the static context -
'max'.
What is wrong?
Thanks a lot,
Peter
Use the well known maximum idiom:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:essox="urn:essox-scripts">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="Field001_max_dluznych_splatek">
<xsl:param name="CrRep"/>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:call-template name="maximun">
<xsl:with-param name="pSequence"
select="$CrRep
/Response
/ContractData
/Installments
/InstDetail
/NrOfDueInstalments"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:call-template name="maximun">
<xsl:with-param name="pSequence"
select="$CrRep
/Response
/ContractData
/Cards
/CardDetail
/NrOfDueInstalments"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$PocetDluznychSplatekSplatky
>= $PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="maximun">
<xsl:param name="pSequence"/>
<xsl:for-each select="$pSequence">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note: In a named template for reuse.
There is no max function in XSLT 1.0. You can work around this by sorting your elements in descending order and then taking the value of the first one.
Here's another (slower) way to do it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="PocetDluznychSplatekSplatky"
select="/test/PocetDluznychSplatekSplatky/val[not(../val > .)][1]" />
<xsl:variable name="PocetDluznychSplatekKarty"
select="/test/PocetDluznychSplatekKarty/val[not(../val > .)][1]" />
<xsl:template match="/">
<xsl:choose>
<xsl:when
test="$PocetDluznychSplatekSplatky >=
$PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sample source document:
<test>
<PocetDluznychSplatekSplatky>
<val>22</val>
<val>3241</val>
<val>13</val>
</PocetDluznychSplatekSplatky>
<PocetDluznychSplatekKarty>
<val>1</val>
<val>3234341</val>
<val>13</val>
</PocetDluznychSplatekKarty>
</test>
Output:
3234341
The XPath in each variable's select looks like this:
/test/PocetDluznychSplatekSplatky/val[not(../val > .)][1]
Or, select the val element having no val siblings with a greater value (i.e. the max).
(Obviously, you'll need to adjust the XPath to fit your source document.)
Note: The sort solution performs much better (assuming an n*log(n) sort implementation). The second approach needs to compare each val to every one of its siblings and is therefore quadratic.