How do I write a predicate for this in a single xpath? - xslt-2.0

I'm trying to write a function to return a value based on attributes that adhere to a certain logic.
if <Position isAdded="1"> exists then this is what I need.
if <Position isAdded="1"> doesn't exist, and <Position isUpdated=1> exists, that I need this element.
I never should pull <Position isDeleted=1>
If isAdded and isUpdated don't exist, then I need to get the value from
<Position> without any attributes.
This is what I've tried so far
/Root/Effective_Change/Position[not(exists(#isDeleted))][exists(#isAdded)]
get's me the isAdded element.
I can add multiple choose when and test for every attribute. I'm just
trying to see if this can be done in a single xpath. Since there are a lot of elements in my xml, I don't want to do this for every element. Hence
trying to build a function.
Like
/Root/Effective_Change/Position[if (exists(#isAdded then
.[#isAdded]
else if exists(#isUpdated) then
.[#isUpdated] else not(exists(#isDeleted))]
Sample xml
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Effective_Change Sequence="1">
<Position isUpdated="1">
<Business_Title>Updated Product Engineer</Business_Title>
<Worker_Type>Regular</Worker_Type>
</Position>
<Position isAdded="1">
<Business_Title>Product Engineer</Business_Title>
<Worker_Type>Regular</Worker_Type>
</Position>
<Position isDeleted="1">
<Business_Title>Deleted Product Engineer</Business_Title>
<Worker_Type>Regular</Worker_Type>
</Position>
<Position>
<Business_Title>Product Engineer</Business_Title>
<Worker_Type>Regular</Worker_Type>
</Position>
</Effective_Change>
</Root>
The actual file can have these attributes at multiple levels. for
example:
<Position isUpdated="1">
<Business_Title>Updated Product Engineer</Business_Title>
<Worker_Type isDeleted="1">Temporary</Worker_Type>
<Worker_Type isAdded="1">Regular</Worker_Type>
</Position>
I use this function to get non-deleted data
<xsl:function name="this:getNonDeletedElement">
<xsl:param name="element"/>
<xsl:variable name="nodeName">
<xsl:value-of select="$element[1]/name()"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="exists($element/ancestor::*[name() = 'Effective_Change']//*[not(exists(#isDeleted))]/*[name() = $nodeName][not(exists(#isDeleted))])">
<xsl:value-of select="$element/ancestor::*[name() = 'Effective_Change']//*[not(exists(#isDeleted))]/*[name() = $nodeName][not(exists(#isDeleted))]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
This function, however, would still return isAdded, isUpdated and non-attributed rows. In that case, if isAdded exists, I need that, if isAdded doesnt exist and isUpdated does, then I need that, else the non-attributed row.
I'm using this to check for isAdded
<xsl:function name="this:isElementAdded">
<xsl:param name="element"/>
<xsl:choose>
<xsl:when test="$element/../#isAdded or $element/#isAdded or exists($element/ancestor::*[#isAdded and name(..)='Effective_Change'])">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
I'm wondering if there's a simpler way to doing this.

Related

Return a value type of element from a function in XSLT

This is my XML
<report>
<format-inputs>
<narrative-entity-ids>
<entity id="28495795" type-cdf-meaning="DIAGNOSES"/>
<entity id="28495741" type-cdf-meaning="DIAGNOSES"/>
<entity id="28495471" type-cdf-meaning="DIAGNOSES"/>
</narrative-entity-ids>
</format-inputs>
</report>
I am creating a function in commonFunction.xslt
<xsl:function name="cdocfx:createEntityIdList" >
<xsl:param name="formatInputsNodes"/>
<xsl:if test="fn:exists(n:report/n:format-inputs)"
<xsl:variable name="entityIdList" as="element()*">
<xsl:for-each select="$formatInputsNodes/n:narrative-entity-ids/n:entity">
<Item><xsl:value-of select="#id"/></Item>
</xsl:for-each>
</xsl:variable>
</xsl:if>
<xsl:copy-of select="$entityIdList"/>
</xsl:function>
I am calling this function in other xslt file where commonFunction.xslt was included
<xsl:variable name="entityIdList" select="cdocfx:createEntityIdList(n:report/n:format-inputs)"/>
</xsl:variable>
My question is variable entityIdList should be value type of element but it is having the document-node type how can i achieve this ??
Please provide minimal but complete samples of XML input, XSLT you have, output you want and the one you get together with any exact error messages you have encountered.
I am currently not sure I understand what you want to achieve, if you construct a variable of type element()* you seem to want to construct a sequence of element nodes. Any xsl:value-of however will only output the string values of the selected items in a text node so it is not clear why you first construct elements if you only want to output string values. If you construct nodes and want to output them use xsl:copy-of or xsl:sequence, not xsl:value-of.
To show two examples of writing a function that returns a sequence of elements (i.e. whose result is of type element()*) I have set up https://xsltfiddle.liberty-development.net/3NzcBtE which has two functions
<xsl:function name="mf:ex1">
<xsl:param name="input" as="element()*"/>
<xsl:for-each select="$input">
<item>{ #id }</item>
</xsl:for-each>
</xsl:function>
<xsl:function name="mf:ex2">
<xsl:param name="input" as="element()*"/>
<xsl:variable name="elements" as="element()*">
<xsl:for-each select="$input">
<item>{ #id }</item>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$elements"/>
</xsl:function>
the first simply directly constructs some result elements in the function body, that way the result is a sequence of element nodes. The second function uses your approach of constructing a sequence of elements nodes in a variable, the proper way to return that variable value then from the function is to use xsl:sequence.
It is not clear at which position of the posted code you think are dealing with a document-node() node.
Note also that
<xsl:choose>
<xsl:when test="fn:exists($formatInputsNodes/n:narrative-entity-ids)">
<xsl:for-each select="$formatInputsNodes/n:narrative-entity-ids/n:entity">
<Item><xsl:value-of select="#id"/></Item>
</xsl:for-each>
</xsl:when>
</xsl:choose>
can be simplified to
<xsl:for-each select="$formatInputsNodes/n:narrative-entity-ids/n:entity">
<Item><xsl:value-of select="#id"/></Item>
</xsl:for-each>
As you have now presented an XML input that is at least well-formed and some XSLT snippets (that are unfortunately not well-formed and seem to use a namespace although the XML input shown doesn't use one) here is an attempt to morph that into a working sample
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cdocfx="http://example.com/cdox-functions"
exclude-result-prefixes="#all"
version="3.0">
<xsl:function name="cdocfx:createEntityIdList" >
<xsl:param name="formatInputsNodes"/>
<xsl:variable name="entityIdList" as="element()*">
<xsl:for-each select="$formatInputsNodes/narrative-entity-ids/entity">
<Item><xsl:value-of select="#id"/></Item>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="$entityIdList"/>
</xsl:function>
<xsl:variable name="entityIdList" select="cdocfx:createEntityIdList(report/format-inputs)"/>
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="$entityIdList instance of element()*, $entityIdList" separator=", "/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/pPqsHTW/1
Output there for the check $entityIdList instance of element()* is true so I am not sure why you say you have a document node.

XSLT 2 - pick item from tokenize()'d list by index

My environment is SAXON (last nights build) using XSLT 2.0. My real problem is that the XML document specification is sub-optimal, and in a way, my problem relates to fixing/working around that design issue.
I have a node type (<weaponmodesdata>) where all the direct children are |-separated string lists of 1-or-many elements (each child of the same <weaponmodesdata> will have the same length). I need to go over the various modes represented and "unspin" them out to separate item lists (in plain text), rather than having them all smooshed together.
Unfortunately right now I'm getting a really stubborn
XPTY0020: Required item type of the context item for the child axis is node(); supplied
value has item type xs:string
error on the lines where I pass the node that needs to be split up into my little template.
Currently I have
<xsl:template match="trait" mode="attack">
<xsl:for-each select="tokenize(weaponmodesdata/mode, '\|')">
<xsl:variable name="count" select="position()"/>
<xsl:value-of select="name"/><xsl:text> - </xsl:text>
<xsl:call-template name="split_weaponmode">
<xsl:with-param name="source" select="weaponmodesdata/damage"/>
<xsl:with-param name="item" select="$count"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="split_weaponmode">
<xsl:with-param name="source" select="weaponmodesdata/damtype"/>
<xsl:with-param name="item" select="$count"/>
</xsl:call-template>
<!-- more will go here eventually -->
<xsl:text>.
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="split_weaponmode">
<xsl:param name="source"/>
<xsl:param name="item"/>
<xsl:variable name="parts" select="tokenize($source, '\|')"/>
<xsl:for-each select="$parts">
<xsl:if test="position() = $item">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
An example XML subtree relating to my issue:
<character>
<trait id="1">
<name>Spear</name>
<weaponmodesdata>
<mode>1H Thrust|2H Thrust|Thrown</mode>
<damage>thr+2|thr+3|thr+3</damage>
<damtype>imp|imp|imp</damtype>
</weaponmodesdata>
</trait>
<trait id="2">
<name>Broadsword</name>
<weaponmodesdata>
<mode>1H Thrust|1H Swing</mode>
<damage>thr+1|sw+2</damage>
<damtype>imp|cut</damtype>
</weaponmodesdata>
</trait>
</character>
Example desired output:
Spear - 1H Thrust; thr+2 imp.
Spear - 2H Thrust; thr+3 imp.
Spear - Thrown; thr+3 imp.
Broadsword - 1H Thrust; thr+1 imp.
Broadsword - 1H Swing; sw+2 cut.
One issue (that one causing the error message) with your code is that your for-each operates on a sequence of string value (i.e. inside the for-each body the context item is a string value), yet you have relative XPath expressions like weaponmodesdata/damage that require a context node to makes sense. So you would need to use a variable outside of the for-each to store your context node.
But I think you can simplify your code to
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="trait">
<xsl:variable name="this" select="."/>
<xsl:variable name="count" select="count(tokenize(weaponmodesdata/*[1], '\|'))"/>
<xsl:for-each-group select="weaponmodesdata/*/tokenize(., '\|')" group-by="position() mod $count">
<xsl:value-of select="$this/name"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="current-group()"/>
<xsl:text>.
</xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
If you want to stick with your approach of calling templates then make sure you store the context node of the template using e.g. <xsl:variable name="this" select="."/> so that you can access it inside of the for-each iterating over a string item.

Unsure whether I need a group or a sort or something else

Hi I am an occasional user of XSLT so am probably missing something obvious, but hopefully someone can point it out!
The original XML has the structure;
<test>
<input>a</input>
<input>b</input>
<input>c</input>
<input>d</input>
<input>e</input>
</test>
The XSL file contains the following processing commands;
<xsl:template name="convertInputToNumeric">
<xsl:param name="inputs" />
<xsl:for-each select="input">
<NumericCode>
<xsl:call-template name="toNumericCode">
<xsl:with-param name="type">Input</xsl:with-param>
<xsl:with-param name="" select="." />
</xsl:call-template>
</NumericCode>
</xsl:for-each>
</xsl:template>
the call template 'toNumericCode' takes the current input and looks up in another xml file a numeric representation for the input eg the input 'a' returns the value '001'
<Conversion type="Input">
<Convert>
<FROM>a</FROM>
<TO>001</TO>
</Convert>
<Convert>
<FROM>b</FROM>
<TO>002</TO>
</Convert>
<Convert>
<FROM>c</FROM>
<TO>001</TO>
</Convert>
<Convert>
<FROM>d</FROM>
<TO>001</TO>
</Convert>
<Convert>
<FROM>e</FROM>
<TO>002</TO>
</Convert>
</Conversion>
so running the XSL I currently get
<test>
<NumericCode>001</NumericCode>
<NumericCode>002</NumericCode>
<NumericCode>001</NumericCode>
<NumericCode>001</NumericCode>
<NumericCode>002</NumericCode>
</test>
but actually what I want is that I only get the distinct nodes eg
<test>
<NumericCode>001</NumericCode>
<NumericCode>002</NumericCode>
</test>
I don't know how best to do this as I would want to group based on the numeric code value that is returned from the template 'toNumericCode' rather than the initial input value?
You may use distinct-values(). Have look, I have change your shared template with this one:
<xsl:template name="convertInputToNumeric">
<xsl:param name="inputs" />
<xsl:parm name="abc"><xsl:for-each select="input">
<NumericCode>
<xsl:call-template name="toNumericCode">
<xsl:with-param name="type">Input</xsl:with-param>
<xsl:with-param name="" select="." />
</xsl:call-template>
</NumericCode>
</xsl:for-each>
</xsl:parm>
<xsl:for-each select="distinct-values($abc/NumericCode)">
<NumericCode><xsl:value-of select="."/></NumericCode>
</xsl:for-each>
</xsl:template>
output:
<NumericCode>001</NumericCode><NumericCode>002</NumericCode>

Use a dynamic match in XSLT

I have an external document with a list of multiple Xpath like this:
<EncrypRqField>
<EncrypFieldRqXPath01>xpath1</EncrypFieldRqXPath01>
<EncrypFieldRqXPath02>xpath2</EncrypFieldRqXPath02>
</EncrypRqField>
I use this document to obtain the Xpath of the nodes I want to be modified.
The input XML is:
<Employees>
<Employee>
<id>1</id>
<firstname>xyz</firstname>
<lastname>abc</lastname>
<age>32</age>
<department>xyz</department>
</Employee>
</Employees>
I want to obtain something like this:
<Employees>
<Employee>
<id>XXX</id>
<firstname>xyz</firstname>
<lastname>abc</lastname>
<age>XXX</age>
<department>xyz</department>
</Employee>
</Employees>
The XXX values are the result of a data encryption, I want to dynamically obtain the Xpath from the document and change the value of its node.
Thanks.
I'm not sure if something like this is possible in XSL 2.0. May be in 3.0 there should be some function evaluate() but I don't know any details.
But I tried some workaround and it seems to be functional. Of course it is not perfect and has many limitations in this form (e.g. you need to specify absolute path, you cannot use more complex XPath like //, [], etc.) so consider it just as an idea. But it could be the way in some easier cases.
It is based on comparing of two string instead of evaluation string as XPath.
Simplified xml with xpaths to encrypt (I ommit the number for simplicity).
<?xml version="1.0" encoding="UTF-8"?>
<EncrypRqField>
<EncrypFieldRqXPath>/Employees/Employee/id</EncrypFieldRqXPath>
<EncrypFieldRqXPath>/Employees/Employee/age</EncrypFieldRqXPath>
</EncrypRqField>
And my transformation
<xsl:template match="element()">
<xsl:variable name="pathToElement">
<xsl:call-template name="getPath">
<xsl:with-param name="element" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$xpaths/EncrypFieldRqXPath[text() = $pathToElement]">
<!-- If exists element with exacty same value as constructed "XPath", ten "encrypt" the content of element -->
<xsl:copy>
<xsl:text>XXX</xsl:text>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- This template will "construct" the XPath for element under investigation. -->
<!-- There might be an easier way (e.g. some build-in function), but it is actually out of my skill. -->
<xsl:template name="getPath">
<xsl:param name="element" />
<xsl:choose>
<xsl:when test="$element/parent::node()">
<xsl:call-template name="getPath">
<xsl:with-param name="element" select="$element/parent::node()" />
</xsl:call-template>
<xsl:text>/</xsl:text>
<xsl:value-of select="$element/name()" />
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Saxon-ce dynamic xpath evaluation

I am trying to evaluate a dynamic xpath using ixsl:eval() in Saxon-ce with xslt 2.0 but does not seem to be working. Here is the illustrative XML
<things>
<thing>
<name>widget one</name>
<number>10</number>
<type>metal</type>
<subtypes>
<subtype>red<subtype>
</subtypes>
</thing>
<thing>
<name>widget two</name>
<number>11</number>
<type>wood</type>
<subtypes>
<subtype>red</subtype>
<subtype>blue</subtype>
</subtypes>
</thing>
</things>
and a piece of the xsl 2.0 style sheet I am trying to eval (the various parameters are passed by another part of the larger xsl stylesheet)
<template name="display" match="things">
<xsl:param name="num"/>
<xsl:param name="type" as="xs:string"/>
<xsl:param name="subtype" as="xs:string"/>
<xsl:variable name="xpathExp" as="xs:string">
<xsl:text>things/thing</xsl:text>
<xsl:if test="not($num = 'all')>
<xsl:copy-of select="concat('[number=',$num,']')"/>
</xsl:if>
<xsl:if test="not($type = 'all')>
<xsl:copy-of select="concat('[type=''',$type,''']')"/>
</xsl:if>
<xsl:if test="note($subtype = 'all')>
<xsl:copy-of select="concat('[subtype/subtypes=''',$subtype,''']')/>
</xsl:if>
</xsl:variable>
<xsl:result-document href="#display" method="ixsl:replace-content">
<xsl:for-each select="ixsl:eval($xpathExp)">
<xsl:sort select="name"/>
</xsl:for-each>
</template>
When I replace the eval statement with a explicit xpath statement the code works, so for some reason the eval on $xpathExp is not working. Ideas?
* Edit *
Here is a better XML example:
<things>
<thing>
<name>widget one</name>
<number>10</number>
<type>metal</type>
<subtypes>
<subtype>red<subtype>
</subtypes>
</thing>
<thing>
<name>widget two</name>
<number>11</number>
<type>wood</type>
<subtypes>
<subtype>red</subtype>
<subtype>blue</subtype>
</subtypes>
</thing>
<thing>
<name>widget three</name>
<number>11</number>
<type>metal</type>
<subtypes>
<subtype>blue</subtype>
</subtypes>
</thing>
</things>
The user can select values via dropboxes for number, type and subtype. Depending on the user's selection, a list of thing names is displayed. So for example, if the user selects a number of 11 and the subtype of red it would just display widget two. If they select instead a subtype of blue it would display the name of widgets two and three.
So the base xpath filter is things/thing. If the user selects a number value I want to append [number=$num] to the xpath expression so ti would be things/thing[number=$num]. If they select more than one item, lets say number and type, [number=$num][type=$type] would be appended to the base xpath and I would have things/thing[number=$num][type=$type].
Basically what I trying to avoid is having to individually code all possible permutations and combinations of possible user selections.
Does that help?
The ixsl:eval function is for dynamic evaluation of JavaScript, not XPath. From the Saxon-CE documentation:
...Executes Javascript code, supplied as a string. The supplied script is
injected into the DOM as a function which is evaluated immediately to
return a result.
For the code sample you provide, dynamic evaluation is not required. Only certain classes of XSLT application actually require dynamic XPath evaluation.
In the few cases where dynamic XPath evaluation is required, Saxon-CE has no built-in capability, so instead of just building an XPath dynamically, you would need to generate a simple 'wrapper' XSLT and execute this using the Saxon-CE JavaSCript API. This technique is used in the PathEnq online XPath 2.0 evaluator.
The following code achieves something similar to your code using a single 'static' XPath expression - in this example, a further template is applied to the required 'thing' element to create usable HTML:
rendered code (syntax-highlighted and formatted)
source code:
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
xmlns:js="http://saxonica.com/ns/globalJS"
xmlns:prop="http://saxonica.com/ns/html-property"
xmlns:style="http://saxonica.com/ns/html-style-property"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs prop"
extension-element-prefixes="ixsl"
version="2.0"
>
<xsl:template name="main" match="/">
<xsl:call-template name="display"/>
</xsl:template>
<xsl:template name="display">
<xsl:param name="num" as="xs:string" select="'none'"/>
<xsl:param name="type" as="xs:string" select="'all'"/>
<xsl:param name="subtype" as="xs:string" select="'red'"/>
<xsl:variable name="thingsEl" select="things" as="element(things)"/>
<xsl:variable name="widget" as="element()+"
select="if ($num eq 'all')
then $thingsEl/thing[number = $num]
else if ($type ne 'all')
then $thingsEl/thing[type = $type]
else $thingsEl/thing[subtypes/subtype = $subtype]"/>
<xsl:result-document href="#display" method="append-content">
<xsl:apply-templates select="$widget">
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:result-document>
</xsl:template>
<xsl:template match="thing">
<ol>
<li>name: <xsl:value-of select="name"/></li>
<li>number: <xsl:value-of select="number"/></li>
<li>type: <xsl:value-of select="type"/></li>
<li>subtypes:
<ol>
<xsl:apply-templates select="subtypes/subtype"/>
</ol>
</li>
</ol>
</xsl:template>
<xsl:template match="subtype">
<li><xsl:value-of select="."/></li>
</xsl:template>
</xsl:transform>
The HTML output (outerHTML view of the 'display' div element)
<div id="display">
<ol>
<li>name: widget one</li>
<li>number: 10</li>
<li>type: metal</li>
<li>subtypes:
<ol>
<li>red</li>
</ol></li>
</ol>
<ol>
<li>name: widget two</li>
<li>number: 11</li>
<li>type: wood</li>
<li>subtypes:
<ol>
<li>red</li>
<li>blue</li>
</ol></li>
</ol>
</div>

Resources