XSLT Optimizing xsl:choose and str:tokenize - xslt

I have been researching and haven't been able to find anything related to optimizing XSLT. Below is the snippet that I am working on and wanted to see if anything can be done to help with the xslt transformation.
<xsl:template match="a:OBR/*">
<xsl:choose>
<xsl:when test ="name() = 'OBR-10' and string-length(.) = 0">
<OBR-10>USER</OBR-10>
</xsl:when>
<xsl:when test ="name() = 'OBR-18'">
<OBR-18>
<xsl:value-of select ="//a:PV1/a:PV1-44"/>
</OBR-18>
</xsl:when>
<xsl:when test ="name() = 'OBR-19'">
<OBR-19>
<xsl:if test = "string-length(str:tokenize(../a:OBR-18,'^')[5]) > 0">
<xsl:value-of select ="str:tokenize(../a:OBR-18,'^')[5]"/>
</xsl:if>
</OBR-19>
</xsl:when>
<xsl:when test ="name() = 'OBR-33'">
<OBR-33>
<xsl:value-of select ="translate(../parent::a:ORC[1]/a:ORC-4,'^','~')"/>
</OBR-33>
</xsl:when>
<xsl:when test="name()='NTE'">
<NTE>
<xsl:apply-templates/>
</NTE>
</xsl:when>
<xsl:when test="name()='DG1'"/>
<!--<DG1>
<xsl:apply-templates/>
</DG1>
</xsl:when>-->
<xsl:when test="name()='OBX'">
<OBX>
<xsl:apply-templates/>
</OBX>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

I would suggest to write code like
<xsl:template match="a:OBR/*">
<xsl:choose>
<xsl:when test ="name() = 'OBR-10' and string-length(.) = 0">
<OBR-10>USER</OBR-10>
</xsl:when>
as
<xsl:template match="a:OBR/OBR-10[string-length() = 0]">
<xsl:copy>USER</xsl:copy>
</xsl:template>
or perhaps
<xsl:template match="a:OBR/OBR-10[. = '']">
<xsl:copy>USER</xsl:copy>
</xsl:template>
that is, to write templates that match each element by its name and if needed a predicate/condition instead of that odd approach to match on * and then test the name. I don't see that necessarily as an optimization (you would have to measure with a particular implementation) but as a clear and modular coding style.
The
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
would then be code as
<xsl:template match="a:OBR/*">
<xsl:copy-of select="."/>
</xsl:template>
or probably already covered by an identity transformation template set up as the starting point to initiate and keep up the processing.
You would have to show the namespaces in the input document and the XSLT to allow a precise suggestion in terms of namespaces (could be that you want/need xsl:template match="a:OBR/a:OBR-10[string-length() = 0]").

Related

Passing node and field dynamically to xslt function

I am trying to make something to act as a null check decimal fields for use in xslt transformations. I was thinking I could also make similar for other data types as I need. I made this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.workday.report/CR_Question"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:this="urn:this-stylesheet"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:function name="this:NullCheckField" as="xsd:string">
<xsl:param name="node" as="xsd:string"/>
<xsl:param name="fieldName" as="xsd:string"/>
<xsl:variable name="variableName">
<xsl:choose>
<xsl:when test="not(exists(concat($node, '/', $fieldName)))">
<xsl:value-of select="format-number(0, '#.00')"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="x">
<xsl:value-of select="concat($node, '/', $fieldName)"/>
</xsl:variable>
<xsl:variable name="y">
<xsl:value-of select="$x"/>
</xsl:variable>
<xsl:value-of select="format-number(number($y), '#.00')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$variableName"/>
</xsl:function>
<xsl:output method="text" indent="yes"/>
<xsl:template match="wd:Report_Entry">
<xsl:value-of select="this:NullCheckField('wd:Report_Data/wd:Report_Entry','wd:deduction_amount')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
and i tried calling it with this line
<wd:Report_Data xmlns:wd="urn:com.workday.report/CR_Question">
<wd:Report_Entry>
<wd:Employee_ID>1555111</wd:Employee_ID>
<wd:Check_Date>2022-04-28</wd:Check_Date>
<wd:NegDeductIndc>0</wd:NegDeductIndc>
<wd:Deduction_YTD>0</wd:Deduction_YTD>
</wd:Report_Entry>
<wd:Report_Entry>
<wd:Employee_ID>1555222</wd:Employee_ID>
<wd:Check_Date>2022-04-28</wd:Check_Date>
<wd:NegDeductIndc>0</wd:NegDeductIndc>
<wd:Deduction_YTD>0</wd:Deduction_YTD>
</wd:Report_Entry>
</wd:Report_Data>
When I run this, the test should fail and use the hard coded 0 since the node is not there, but instead of dropping into
<xsl:when test="not(exists(concat($node, '/', $fieldName)))">
<xsl:value-of select="format-number(0, '#.00')"/>
</xsl:when>
It keeps dropping into the and failing out. I am not sure of two things.
The first is why is my exist check not working correctly and falling into the
Other question - am I able to pass the node names and concat them like this?
Edit: Updated function as of now
<!-- param type 1 = decimal -->
<!-- param type 2 = integer -->
<!-- param type 3 = string -->
<xsl:function name="this:NullCheckField" as="xsd:string">
<xsl:param name="node"/>
<xsl:param name="type" as="xsd:integer"/>
<xsl:variable name="variableName">
<xsl:choose>
<xsl:when test="not(exists($node))">
<xsl:choose>
<xsl:when test="$type = 1">
<xsl:value-of select="format-number(0, '#.00')"/>
</xsl:when>
<xsl:when test="$type = 2">
<xsl:value-of select="0"/>
</xsl:when>
<xsl:otherwise>
<xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="x">
<xsl:value-of select="$node[0]"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$type = 1">
<xsl:value-of select="format-number(number($x), '#.00')"/>
</xsl:when>
<xsl:when test="$type = 2">
<xsl:value-of select="number($x)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$x"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$variableName"/>
</xsl:function>
The problem I am trying to resolve is my integration will not always produce the empty nodes. Sometimes there is a missing node and I have been doing checks like this
<xsl:choose>
<xsl:when test="not(exists(wd:DPDEEYDEDS))">
<xsl:value-of select="' '"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="wd:DPDEEYDEDS"/>
</xsl:otherwise>
</xsl:choose>
Some of these are decimal, some are integer, some are spaces. I'm trying to do a dynamic null check. The function will be called, the field node evaluated, if it is not a missing or empty node then it formats the value, otherwise return a default 0|0.00|' '.
Unless you want to move to XSLT with xsl:evaluate support the whole idea of passing in strings with XPath fragments is nonsense.
If, in the context of template match="wd:Report_Entry", you want to pass that Report_Entry element to a function use . e.g. this:my-function(., 'deduction_amount'). The first function argument should be of type node() or element(), certainly you don't want to atomize the node to a string.
Then you could check e.g. test="$node/*[local-name() = $fieldName]" to check for the existence of a child element with local name $fieldName.
I don't see any need to work with element names as as strings, you can simply pass in e.g. this:my-function(wd:deduction_amount), with the type of the paramter being node()? or node()* and then do any exists check on that, if needed.
The whole construction of nested, untyped variables is unnecessarily complicated and inefficient.
I don't even see the need for the function, doing e.g. format-number((wd:deduction_amount, 0)[1], '#.00') inside the template suffices.

idiomatic alternative to choose -> test -> value-of (XSLT 1.0)

In the work I do I seem to see a lot of code liek this..
<xsl:choose>
<xsl:when test="long_xpath_to_optional/#value1">
<xsl:value-of select="long_xpath_to_optional/#value"/>
</xsl:when>
<xsl:when test="another_long_xpath_to_optional/#value">
<xsl:value-of select="another_long_xpath_to_optional/#value"/>
</xsl:when>
<etc>
</etc>
<otherwise>
<xsl:value-of select="default_long_xpath_to_value"/>
</otherwise>
</xsl:choose>
its very long and very repetitive.
When I'm were working in some other (psuedo) language I would go
let values = concat(list(long_xpath_to_optional_value),list(another_long_xpath_to_optional_value))
let answer = tryhead(values,default_long_xpath_to_value)
i.e. create a list of values in priority order, and then take the head.
I only evaluate each path once
how would you do something similar in XSLT 1.0 (we can use node-sets).
I was wondering if you can create a node-set somehow
You can - but it's not going to be any shorter:
<xsl:variable name="values">
<xsl:apply-templates select="long_xpath_to_optional/#value" mode="values"/>
<xsl:apply-templates select="another_long_xpath_to_optional/#value" mode="values"/>
<xsl:apply-templates select="default_long_xpath_to_value/#value" mode="values"/>
</xsl:variable>
<xsl:value-of select="exsl:node-set($values)/value[1]" xmlns:exsl="http://exslt.org/common"/>
and then:
<xsl:template match="#value" mode="values">
<value>
<xsl:value-of select="."/>
</value>
</xsl:template>
But at least the repetition is eliminated.
Alternatively, you could do:
<xsl:template match="#value" mode="values">
<xsl:value-of select="."/>
<xsl:text>|</xsl:text>
</xsl:template>
and then:
<xsl:value-of select="substring-before($values, '|')"/>
To use variables you write
<xsl:variable name="value1" select="long_xpath_to_optional/#value1"/>
<xsl:variable name="value2" select="another_long_xpath_to_optional/#value"/>
<xsl:variable name="value3" select="default_long_xpath_to_value"/>
and then in XPath 2 or 3 all you would need is ($value1, $value2, $value3)[1] or head(($value1, $value2, $value3)) but in XSLT 1 with XPath 1 all you can write as a single expression is ($value1 | $value2 | $value3)[1] which sorts in document order so unless the document order is the same as your test order this wouldn't work to check the values; rather you would need to maintain the
<xsl:choose>
<xsl:when test="$value1">
<xsl:value-of select="$value1"/>
</xsl:when>
<xsl:when test="$value2">
<xsl:value-of select="$value2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value3"/>
</xsl:otherwise>
</xsl:choose>
Of course in XPath 2 you wouldn't really need the variables and could use (long_xpath_to_optional/#value1, another_long_xpath_to_optional/#value, default_long_xpath_to_value)[1] as well directly.

Check if a map is empty in xslt

I create a map variable and want to check if it has items.
<xsl:variable as="map(xs:string, xs:string)*" name="ancestorsMap">
<xsl:map>
<xsl:for-each select="$nodes">
<xsl:variable name="ancestor" select="(ancestor::node()[not(descendant-or-self::layer)])[1]/#xml:id"/>
<xsl:if test="exists($ancestor)">
<xsl:map-entry key="string(#xml:id)" select="string($ancestor)"/>
</xsl:if>
</xsl:for-each>
</xsl:map>
</xsl:variable>
<xsl:variable name="check">
<xsl:choose>
<xsl:when test="empty($ancestorsMap)">
<xsl:value-of select="'NaN'"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$ancestorsMap"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
When it is empty, the oXygen variable panel shows:
Value type: map(*)1
Value: map{}
I tried fn:empty() so far
It looks like
test="map:size($ancestorsMap) = 0"
works fine. But maybe there are better approaches.

XSLT - Format-number output NaN

I'm new to XSLT. I'm working with zip codes and I'm trying to padleft zeros to any zipcode under 5 characters. Otherwise, I want my code to simply write the input parameter exactly as is. I'm running into a problem when the zipcode starts with or contains a letter. My output is returning a NaN. How do I tell the code that whenever the zipcode contains a letter, to simply write out the zipcode as is without running the "format-number" logic? I know about the "starts-with" and "contain" functions but I don't totally understand how they work.
<xsl:template name="MyZipCodeUnder5DigitsPadLeftZerosTemplate">
<xsl:param name="BillToZipcode" />
<xsl:choose>
<xsl:when test="string-length($BillToZipcode) < 5">
<xsl:element name="PostalCode">
<xsl:value-of select="format-number($BillToZipcode, '00000')" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="PostalCode">
<xsl:value-of select="$BillToZipcode"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
How about:
<xsl:template name="MyZipCodeUnder5DigitsPadLeftZerosTemplate">
<xsl:param name="BillToZipcode" />
<PostalCode>
<xsl:choose>
<xsl:when test="number($BillToZipcode)">
<xsl:value-of select="format-number($BillToZipcode, '00000')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$BillToZipcode"/>
</xsl:otherwise>
</xsl:choose>
</PostalCode>
</xsl:template>
This assumes no (numerical) Zipcode can have more than 5 digits. If this is not true, you could use:
<xsl:when test="number($BillToZipcode) < 10000">

anchor tag is not getting created correctly

Hi i've the below xslt for creating anchor tags.
<xsl:template match="para/text()">
<xsl:variable name="numx">
<xsl:number format="1" level="any"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="(contains(substring(substring-after(current(),'.'),4,1),')') or contains(substring(substring-after(current(),'.'),4,1),'.') or contains(substring(substring-after(current(),'.'),4,1),' ')) and (contains(substring (current(),string-length(substring-before(current(), '.')) -1,2),' ')) and contains(substring(current(),string-length(substring-before(current(), '.')) -2,1),$numx)">
<xsl:variable name="before">
<xsl:value-of select="normalize-space(substring(current(),string-length(substring-before(current(), '.')) -1,2))"/>
</xsl:variable>
<xsl:variable name="NewN">
<xsl:value-of select="concat('0',$before)"/>
</xsl:variable>
<xsl:variable name="after">
<xsl:value-of select="substring(substring-after(current(),'.'),1,3)"/>
</xsl:variable>
<xsl:variable name="befdNumb">
<xsl:value-of select="substring-before(current(),$before)"/>
</xsl:variable>
<xsl:variable name="aftdNumb">
<xsl:value-of select="substring-after(current(),$after)"></xsl:value-of>
</xsl:variable>
<xsl:value-of select="$befdNumb"/>
<xsl:text> </xsl:text>
<a href="{concat('er:#CLI_CH_',$NewN,'/','P',normalize-space($before),'-',$after)}">
<xsl:value-of select="concat(normalize-space($before),'.',$after)"/>
</a>
<xsl:value-of select="$aftdNumb"/>
</xsl:when>
<xsl:when test="(contains(substring(substring-after(current(),'.'),4,1),')') or contains(substring(substring-after(current(),'.'),4,1),'.') or contains(substring(substring-after(current(),'.'),4,1),' ')) and contains(substring (current(),string-length(substring-before(current(), '.')) -2,1),' ')">
<xsl:variable name="before">
<xsl:value-of select="substring(current(),string-length(substring-before(current(), '.')) -2,3)"/>
</xsl:variable>
<xsl:variable name="NewN">
<xsl:value-of select="$before"/>
</xsl:variable>
<xsl:variable name="after">
<xsl:value-of select="substring(substring-after(current(),'.'),1,3)"/>
</xsl:variable>
<xsl:variable name="befdNumb">
<xsl:value-of select="substring-before(current(),$before)"/>
</xsl:variable>
<xsl:variable name="aftdNumb">
<xsl:value-of select="substring-after(current(),$after)"></xsl:value-of>
</xsl:variable>
<xsl:value-of select="$befdNumb"/>
<xsl:text> </xsl:text>
<a href="{concat('er:#CLI_CH_',$NewN,'/','P',normalize-space($before),'-',$after)}">
<xsl:value-of select="concat(normalize-space($before),'.',$after)"/>
</a>
<xsl:value-of select="$aftdNumb"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
but this xslt is getting applied i.e. the anchor tag is getting created if the text is like below.(having only '.' in the entire text).
serving on the company, by one of the means discussed at paragraph 12.012 below, a demand signed by the creditor
but i want it o be applied for the below text also
<para>the major issues here concern the notion of principal and ancillary jurisdictions, the ideal being that the ancillary jurisdiction will defer to the principal jurisdiction on most important matters, with a view to bringing about a just, practical and economically rational winding-up of affairs. A relatively recent development in connection with that ideal concerns the judicial promotion of court-endorsed agreements known as "crossborder protocols" between liquidators and similar officers appointed in different jurisdictions. It is important in this context, however, not to lose sight of the fact that certain matters of "administration" always remain governed by Hong Kong law. See paragraphs 12.016 to 12.032 below.</para>
please let me know how do i do it. i need to convert this number as er:#CLI_CH_12/P12-016 and er:#CLI_CH_12/P12-032
Thanks
I'm quite sure there could be a better way dot this, but because it is not entirely clear what this xslt should do I stay with your solution.
You need to do some recursive template calls.
Change your current "para/text()" template to a named template with text as an parameter.
But replace every current() with $text
<xsl:template name="mytext">
<xsl:param name="text" />
<xsl:variable name="numx">
<xsl:number format="1" level="any"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="(contains(substring(substring-after($text,'.'),4,1),')')
or contains(substring(substring-after($text,'.'),4,1),'.')
or contains(substring(substring-after($text,'.'),4,1),' '))
and (contains(substring ($text,string-length(substring-before($text, '.')) -1,2),' '))
and contains(substring($text,string-length(substring-before($text, '.')) -2,1),$numx)">
....
</xsl:template>
Add a new template to call the named one with the current text().
<xsl:template match="para/text()">
<xsl:call-template name="mytext" >
<xsl:with-param name="text" select="." />
</xsl:call-template>
</xsl:template>
Add an when just before the otherwise to output the text before a not handled dot and call the named template with text behind this dot.
<xsl:when test="contains(substring-after($text,'.'),'.')">
<xsl:value-of select="substring-before($text,'.')"/>
<xsl:text>.</xsl:text>
<xsl:call-template name="mytext">
<xsl:with-param name="text" select="substring-after($text,'.')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>