I have XML form data coming in from a vendor app that allows me to select values for fields on the form by using this format.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="text" /><xsl:template match="/"><xsl:apply-templates/></xsl:template>
<xsl:template match="DataSet/diffgr:diffgram/Forms">
<xsl:for-each select="Form">
<xsl:value-of select="FieldName1"/>
<xsl:value-of select="FieldName2"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I can't see the XML, but I can surmise from this that all of the fields are siblings within the <form> and looks something like this:
<form>
<CosignerName1 value="John Doe"/>
<CosignerDate1 value="1/1/1970"/>
<CosignerTime1 value="8:46 PM"/>
<CosignerName2 value="Jane Smith"/>
<CosignerDate2 value="2/2/1972"/>
<CosignerTime2 value="11:46 AM"/>
...
<CosignerName12 value="Will Hunting"/>
<CosignerDate12 value="12/12/1982"/>
<CosignerTime12 value="1:00 AM"/>
</form>
<form>
<CosignerName1 value="Bill Thomas"/>
<CosignerDate1 value="5/5/2020"/>
<CosignerTime1 value="8:46 PM"/>
<CosignerName2 value="Bev Poole"/>
<CosignerDate2 value="6/6/2022"/>
<CosignerTime2 value="11:46 AM"/>
...
<CosignerName12 value="Bob Ross"/>
<CosignerDate12 value="12/12/1982"/>
<CosignerTime12 value="1:00 AM"/>
</form>
Known factors:
Each form has 12 cosigners, each with numbered field names i.e. CosignerName1, CosignerName2, etc.
Each named cosigner also has a CosignerDate and a CosignerTime field associated by number. i.e. CosignerName1 has CosignerDate1 and CosignerTime1 fields that, when combined, show when that user signed the form.
I am using the word 'idnum' in my code and in this question to refer to the number at the end of each label that shows they are part of a set i.e CosignerName1, CosignerDate1, and CosignerTime1 all share idnum=1
Goal: Identify and select only the most recent CosignerName, based on CosignerDate and CosignerTime.
To do this, I figure I need to:
Find the most recent CosignerDate/CosignerTime combination
Read the label of either of the identified fields to extract the idnum from the end
Use the idnum to to identify the correct CosignerName and output it
I have some pieces of the solution, but no way to bring them together:
Create a Timestamp
I can combine a set of CosignerDate[idnum] and CosignerTime[idnum] fields, then translate them into a timestamp. If I could create an array of these timestamps, then I could sort and find the most recent one, but as I understand it XSLT 1.0 doesn't have arrays. So I am not sure what to do with this.
<xsl:variable name="mm" select="substring-before(CosignerDate1,'/')" />
<xsl:variable name="mmyyyy" select="substring-after(CosignerDate1,'/')"/>
<xsl:variable name="dd" select="substring-before($mmyyyy,'/')" />
<xsl:variable name="yyyy" select="substring-after($mmyyyy,'/')" />
<xsl:variable name="ampm" select="substring-after(CosignerTime1,' ')" />
<xsl:variable name="time">
<xsl:choose>
<xsl:when test="$ampm = 'AM'">
<xsl:value-of select="number(translate(substring-before(CosignerTime1,' '),':',''))" />
</xsl:when>
<xsl:when test="$ampm = 'PM'">
<xsl:value-of select="number(translate(substring-before(CosignerTime1,' '),':',''))+number('1200')" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat('Timestamp:',$yyyy,$mm,$dd,$time)"/>
The code above outputs something like: Timestamp:202209191128
Get idnums
I can loop through all of the Cosigners and extract the identifying number shared by their labels.
<xsl:for-each select="*[starts-with(local-name(), 'CosignerName')]">
<xsl:variable name="idnum">
<xsl:value-of select="translate(local-name(),'CosignerName','')" />
</xsl:variable>
<xsl:value-of select="$idnum"/>
<xsl:value-of select="','"/>
</xsl:for-each>
This code above outputs something like: 1,4,7,12
But, this is where I get lost.
Failed Attempt
I tried something like the code below. The logic was to loop through each Cosigner name (12 iterations), get the idnum from the label, use that to get the CosignerDate and CosignerTime, create a timestamp, then sort the timestamps, if the timestamp we are looking at is the top one (most recent) then use the idnum to select the correct CosignerName and output it.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="text" /><xsl:template match="/"><xsl:apply-templates/></xsl:template>
<xsl:template match="DataSet/diffgr:diffgram/Forms">
<xsl:for-each select="Form">
<xsl:for-each select="*[starts-with(local-name(), 'CosignerName')]">
<xsl:variable name="idnum">
<xsl:value-of select="translate(local-name(),'CosignerName','')" />
</xsl:variable>
<xsl:variable name="mm" select="substring-before(concat(CosignerDate,$idnum),'/')" />
<xsl:variable name="mmyyyy" select="substring-after(concat(CosignerDate,$idnum),'/')"/>
<xsl:variable name="dd" select="substring-before($mmyyyy,'/')" />
<xsl:variable name="yyyy" select="substring-after($mmyyyy,'/')" />
<xsl:variable name="ampm" select="substring-after(concat(CosignerTime,$idnum),' ')" />
<xsl:variable name="time">
<xsl:choose>
<xsl:when test="$ampm = 'AM'">
<xsl:value-of select="number(translate(substring-before(concat(CosignerTime,$idnum),' '),':',''))" />
</xsl:when>
<xsl:when test="$ampm = 'PM'">
<xsl:value-of select="number(translate(substring-before(concat(CosignerTime,$idnum),' '),':',''))+number('1200')" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="timestamp" select="concat('Timestamp: ',$yyyy,$mm,$dd,$time)"/>
<xsl:sort select="$timestamp" data-type="number" order="descending"/>
<xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This of course fails for several reasons:
the concat with the $idnum doesn't actually pull values from fields
the sort has to be the first thing in the for-each
the sort has to happen after all of the timestamps are created
the sorted list of timestamps still needs to reference which idnum they were generated from
Without being able to create an array of timestamps and idnums, I don't know how you can accomplish this.
Does anyone know how can I get at the CosignerName[idnum] that is correlated with the most recent CosignerDate[idnum]/CosignerTime[idnum]?
I believe you could just sort the CosignerNameX nodes by the individual components of their associated dates and times, as shown in the two other answers I linked to:
https://stackoverflow.com/a/59288030/3016153
https://stackoverflow.com/a/30631073/3016153
However, it might be more efficient - and certainly more readable - to do this in two steps:
Construct a dateTime value for each cosigner;
Find the cosigner with the most recent value.
Consider the following example:
XML
<DataSet xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<diffgr:diffgram>
<Forms>
<form>
<CosignerName1 value="John Doe"/>
<CosignerDate1 value="1/1/1970"/>
<CosignerTime1 value="8:46 PM"/>
<CosignerName2 value="Jane Smith"/>
<CosignerDate2 value="2/2/1972"/>
<CosignerTime2 value="11:46 AM"/>
<CosignerName12 value="Will Hunting"/>
<CosignerDate12 value="12/12/1982"/>
<CosignerTime12 value="1:00 AM"/>
</form>
<form>
<CosignerName1 value="Bill Thomas"/>
<CosignerDate1 value="5/5/2020"/>
<CosignerTime1 value="8:46 PM"/>
<CosignerName2 value="Bev Poole"/>
<CosignerDate2 value="6/6/2022"/>
<CosignerTime2 value="11:46 AM"/>
<CosignerName12 value="Bob Ross"/>
<CosignerDate12 value="12/12/1982"/>
<CosignerTime12 value="1:00 PM"/>
</form>
</Forms>
</diffgr:diffgram>
</DataSet>
XSLT 1.0 + EXSLT node-set()
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="diffgr exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/DataSet">
<output>
<xsl:for-each select="diffgr:diffgram/Forms/form">
<xsl:variable name="datetimes">
<xsl:for-each select="*[starts-with(local-name(), 'CosignerName')]">
<!-- identify values -->
<xsl:variable name="i" select="substring-after(local-name(), 'CosignerName')" />
<xsl:variable name="date" select="../*[local-name()=concat('CosignerDate', $i)]/#value" />
<xsl:variable name="time" select="../*[local-name()=concat('CosignerTime', $i)]/#value" />
<!-- extract date components -->
<xsl:variable name="year" select="substring-after(substring-after($date, '/'), '/')" />
<xsl:variable name="month" select="substring-before($date, '/')" />
<xsl:variable name="day" select="substring-before(substring-after($date, '/'), '/')" />
<!-- extract time components -->
<xsl:variable name="hour12" select="substring-before($time, ':')" />
<xsl:variable name="minute" select="substring-before(substring-after($time, ':'), ' ')" />
<xsl:variable name="pm" select="contains($time,'PM')" />
<xsl:variable name="hour" select="$hour12 mod 12 + 12*$pm"/>
<!-- construct dateTime -->
<datetime cosigner="{#value}" index="{$i}">
<xsl:value-of select="$year"/>
<xsl:value-of select="format-number($month, '-00')"/>
<xsl:value-of select="format-number($day, '-00')"/>
<xsl:value-of select="format-number($hour, 'T00')"/>
<xsl:value-of select="format-number($minute, ':00')"/>
<xsl:text>:00</xsl:text>
</datetime>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<form>
<xsl:for-each select="exsl:node-set($datetimes)/datetime">
<xsl:sort select="." data-type="text" order="descending"/>
<xsl:if test="position()=1">
<last-cosigner index="{#index}" dateTime="{.}" >
<xsl:value-of select="#cosigner"/>
</last-cosigner>
</xsl:if>
</xsl:for-each>
</form>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<form>
<last-cosigner index="12" dateTime="1982-12-12T01:00:00">Will Hunting</last-cosigner>
</form>
<form>
<last-cosigner index="2" dateTime="2022-06-06T11:46:00">Bev Poole</last-cosigner>
</form>
</output>
P.S. With a Microsoft processor you may need to change the namespace declaration:
xmlns:exsl="http://exslt.org/common"
to:
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
and then change the exsl prefix in lines #5 and #39 to msxsl.
For example I have input string like 000 FF0 F4F CV1. Output for this string should be 000FF0F4F CV1. As you can see only one whitespace character was removed: if there was one whitespace - there is no whitespace at all; if there were two whitespaces - one is removed and one is left (if there were three whitespaces - one is removed and two are left and so on).
More examples:
000 FF0 F4F CV1 -> 000 FF0 F4FCV1
000 FF0 F4F CV1 -> 000FF0 F4FCV1
There is now specified format for input string, you don't know length and number of whitespaces. The only requirement is to remove one whitespace. So generic solution is required. Is there any idea how to implement that using XSLT 1.0?
If - as it seems - you want to remove one character from every group of consecutive whitespace characters (including a group of 1), you could do something like:
XML
<root>
<string>000 FF0 F4F CV1</string>
<string>000 FF0 F4F CV1</string>
<string>000 FF0 F4F CV1</string>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="string">
<xsl:copy>
<xsl:call-template name="process">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="process">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, ' ')">
<xsl:value-of select="substring-before($text, ' ')"/>
<xsl:variable name="tail" select="substring-after($text, ' ')"/>
<xsl:variable name="first-char" select="substring(normalize-space($tail), 1, 1)" />
<xsl:value-of select="substring-before($tail, $first-char)"/>
<!-- recursive call -->
<xsl:call-template name="process">
<xsl:with-param name="text" select="concat($first-char, substring-after($tail, $first-char))" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<string>000FF0F4F CV1</string>
<string>000 FF0 F4FCV1</string>
<string>000FF0 F4FCV1</string>
</root>
Note that this assumes the first character in any group of consecutive whitespace characters is a space. And that the string does not end with a whitespace character.
I am transferring data from one xml format to another with xslt 1.0. In the input xml, address is in a single field as a string separated by spaces. As far as I can tell, it can come in the following formats considering the identifiers in brackets [] are optional.
<Field Name="address"...>number [cardinal-dir] name st-type [unit#]</Field>
OR
<Field Name="address"...>number [cardinal-dir] name st-type APT APT#</Field>
so the data may look like:
"123 3RD AVE" or "123 NE 3RD AVE" or "123 NE 3RD AVE APT 321" ... you get the picture.
And I need it to appropriately populate the following output format:
<mstns:Address>
<mstns:HouseNumber>123</mstns:HouseNumber>
<mstns:StreetName>NE</mstns:StreetName>
<mstns:StreetType>AVE</mstns:StreetType>
<mstns:Apt>APT</mstns:Apt>
</mstns:Address>
I've seen some similar posts about tokenizing strings in xlst 1.0 but combined with the variable input I'm a bit lost.
As I said in the comments, this is an awful lot of work and the quality of the result will depend on how consistent your data is.
Assuming that:
the first word (and only the first word) is the street number;
the second word may be a street direction identifier, from a known list of street direction identifiers;
the street name is followed by a street type identifier, from a known list of street type identifiers,
you could use the following stylesheet as your starting point:
XSLT 1.0 (+EXSLT node-set)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Field[#Name='address']">
<!-- tokenize to words -->
<xsl:variable name="tokens">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="words" select="exsl:node-set($tokens)/token" />
<!-- street direction exists? -->
<xsl:variable name="street-direction-exists" select="$words[2]='N' or $words[2]='E' or $words[2]='S' or $words[2]='W' or $words[2]='NE' or $words[2]='NW' or $words[2]='SE' or $words[2]='SW'" />
<!-- find position of street type -->
<xsl:variable name="street-type-index">
<xsl:call-template name="get-street-type-index">
<xsl:with-param name="words" select="$words"/>
</xsl:call-template>
</xsl:variable>
<!-- output -->
<Address>
<HouseNumber>
<xsl:value-of select="$words[1]"/>
</HouseNumber>
<xsl:if test="$street-direction-exists">
<StreetDirection>
<xsl:value-of select="$words[2]"/>
</StreetDirection>
</xsl:if>
<StreetName>
<xsl:for-each select="$words[1 + $street-direction-exists < position() and position() < $street-type-index]">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</StreetName>
<StreetType>
<xsl:value-of select="$words[number($street-type-index)]"/>
</StreetType>
<xsl:if test="count($words) > $street-type-index">
<Unit>
<xsl:for-each select="$words[position() > $street-type-index]">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</Unit>
</xsl:if>
</Address>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<token>
<xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)" />
</token>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="get-street-type-index">
<xsl:param name="words"/>
<xsl:variable name="street-types">
<street-type>AVE</street-type>
<street-type>STR</street-type>
<!-- add more types here (or use an external XML document for them) -->
</xsl:variable>
<xsl:choose>
<xsl:when test="$words[last()] = exsl:node-set($street-types)/street-type">
<xsl:value-of select="count($words)"/>
</xsl:when>
<xsl:when test="$words">
<!-- recursive call -->
<xsl:call-template name="get-street-type-index">
<xsl:with-param name="words" select="$words[position() != last()]"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Applied to the following test input:
XML
<Fields>
<Field Name="address">123 Old Oak Tree AVE</Field>
<Field Name="address">45B NE Broadway STR</Field>
<Field Name="address">6789 Maple Syrup AVE Room 800</Field>
<Field Name="address">1024 W Three Elm Trees AVE APT 321</Field>
<Field Name="address">1024 False AVE Maria STR Unit 8071</Field>
</Fields>
the result will be:
Result
<?xml version="1.0" encoding="utf-16"?>
<Fields>
<Address>
<HouseNumber>123</HouseNumber>
<StreetName>Old Oak Tree</StreetName>
<StreetType>AVE</StreetType>
</Address>
<Address>
<HouseNumber>45B</HouseNumber>
<StreetDirection>NE</StreetDirection>
<StreetName>Broadway</StreetName>
<StreetType>STR</StreetType>
</Address>
<Address>
<HouseNumber>6789</HouseNumber>
<StreetName>Maple Syrup</StreetName>
<StreetType>AVE</StreetType>
<Unit>Room 800</Unit>
</Address>
<Address>
<HouseNumber>1024</HouseNumber>
<StreetDirection>W</StreetDirection>
<StreetName>Three Elm Trees</StreetName>
<StreetType>AVE</StreetType>
<Unit>APT 321</Unit>
</Address>
<Address>
<HouseNumber>1024</HouseNumber>
<StreetName>False AVE Maria</StreetName>
<StreetType>STR</StreetType>
<Unit>Unit 8071</Unit>
</Address>
</Fields>
Note: You may need to use Microsoft's own node-set() extension function, instead of EXSLT.
I'm trying to match xsl variables i.e. key1, key2 with xml node strings.
Problem: the xsl variables can vary like key1, key2, key3, key4, until key.length...
Question: How can I modify my xsl so when the key[i] is used, then ti will display all the xml node matches.
Here's my XML:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<metadata>
<field>marketing business</field>
<field>PageTitle1 One</field>
<field>marketing business link</field>
<field>planning development</field>
<field>PageTitle2 Two</field>
<field>planning development link</field>
<field>learning development</field>
<field>PageTitle3 Threee</field>
<field>learning development link</field>
</metadata>
</document>
Here's my XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="key1">marketing</xsl:variable>
<xsl:variable name="key2">business</xsl:variable>
<xsl:for-each select="document/metadata/field">
<xsl:choose>
<xsl:when test="contains(.,$key1) and contains(.,$key2)">
match <xsl:value-of select="." /><br/>
</xsl:when>
<xsl:when test="contains(.,$key2)">
match <xsl:value-of select="." /><br/>
</xsl:when>
<!--... add other options here-->
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
Result:
match marketing business
match marketing business link
Any help? or is there a way to put this in an array-like variable or any different approach?...
Consider putting your "keys" in a separate XML document, call it "keys.xml"
<keys>
<key>marketing</key>
<key>business</key>
</keys>
Then, you can create a single variable in your XSLT to reference this document
<xsl:variable name="keys" select="document('keys.xml')/keys" />
With this variable you can then, for example, check if your field element matches all the keys like so:
<xsl:variable name="matches" select="count($keys/key[contains(current(), .)])" />
<xsl:choose>
<xsl:when test="$matches = count($keys/key)">
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:variable name="keys" select="document('keys.xml')/keys" />
<xsl:variable name="totalkeys" select="count($keys/key)" />
<xsl:template match="/">
<xsl:for-each select="document/metadata/field">
<xsl:variable name="matches" select="count($keys/key[contains(current(), .)])" />
<xsl:choose>
<xsl:when test="$matches = $totalkeys">
matches all <xsl:value-of select="." /><br/>
</xsl:when>
<xsl:when test="$matches = 1">
matches one <xsl:value-of select="." /><br/>
</xsl:when>
<xsl:when test="$matches > 0">
matches some <xsl:value-of select="." /><br/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have sample xml like this:
User
name A
Id 8
User
name B
Id 5
User
name C
Id 16
User
name D
Id 10
...
This is what i expect, if input is
A only --> output should be A.
B only --> output should be B.
A and B only --> output should be B.
Any users other than A and B --> output should be name of user with lowest id.
A, B and other users --> user with lowest id except A and B.
I have tried different variants of below template
<xsl:template name="getPriorityName">
<xsl:for-each select="//*[local-name()='User']">
<xsl:sort select="./*[local-name()='Id']" data-type="number"/>
<xsl:variable name="name">
<xsl:value-of select="./*[local-name()='name']"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$name!='A' and $name!='B'">
<xsl:if test="position()=1">
<xsl:value-of select="$name"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
But since i am unable to club name condition with for-each it does not work in case when A and B are there in input
Thanks Jirka :)
I modified the tempalte a little bit and now it looks ok:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Users">
<xsl:variable name="UsersCount" select="count(User)" />
<xsl:variable name="UsersExceptAB" select="User[Name != 'A' and Name !='B']" />
<xsl:variable name="UsersAB" select="User[Name = 'A' or Name ='B']" />
<xsl:variable name="UserInABWithMinimumId" select="$UsersAB[not($UsersAB/Id < ./Id)]" />
<xsl:variable name="UserExceptABWithMinimumId" select="$UsersExceptAB[not($UsersExceptAB/Id < ./Id)]" />
<result>
<xsl:choose>
<xsl:when test="$UsersExceptAB">
<xsl:copy-of select="$UserExceptABWithMinimumId" />
</xsl:when>
<xsl:when test="$UsersAB">
<xsl:copy-of select="$UserInABWithMinimumId" />
</xsl:when>
<xsl:otherwise>
<xsl:comment>Oops, something is wrong.</xsl:comment>
</xsl:otherwise>
</xsl:choose>
</result>
</xsl:template>
</xsl:stylesheet>
Is there a way i can just assign the name to a variable; let's say; priorityName that i can use later, i have some other logic based on this priorityName in my application. I tried to access name from node, but since what we finally have is a node set(always of size 1 - either UserInABWithMinimumId or UserExceptABWithMinimumId). This is what i tried:
<xsl:variable name="priorityName">
<xsl:choose>
<xsl:when test="$UsersExceptAB">
<xsl:value-of select="$UserExceptABWithMinimumId/User/Name" />
</xsl:when>
<xsl:when test="$UsersAB">
<xsl:value-of select="$UserInABWithMinimumId/User/Name" />
</xsl:when>
<xsl:otherwise>
<xsl:comment>Oops, something is wrong.</xsl:comment>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Thanks!!!
You could try following XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Users">
<xsl:variable name="UsersCount" select="count(User)" />
<xsl:variable name="UsersExceptAB" select="User[Name != 'A' and Name !='B']" />
<xsl:variable name="UsersExceptABWithMinimumId" select="$UsersExceptAB[not($UsersExceptAB/Id < ./Id)]" />
<result>
<xsl:choose>
<!-- There exists some User with other name than A or B-->
<xsl:when test="$UsersExceptAB">
<xsl:copy-of select="$UsersExceptABWithMinimumId" />
</xsl:when>
<!-- There exists no User with other than A or B but there is a User with name B -->
<xsl:when test="User[Name = 'B']">
<xsl:copy-of select="User[Name = 'B']" />
</xsl:when>
<!-- There is no User with other than A -->
<xsl:when test="User[Name='A']">
<xsl:copy-of select="User[Name = 'A']" />
</xsl:when>
<!-- Probably there is no User -->
<xsl:otherwise>
<xsl:comment>Oops, something is wrong.</xsl:comment>
</xsl:otherwise>
</xsl:choose>
</result>
</xsl:template>
</xsl:stylesheet>
When I used this input
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User>
<Name>A</Name>
<Id>8</Id>
</User>
<User>
<Name>B</Name>
<Id>9</Id>
</User>
<User>
<Name>C</Name>
<Id>6</Id>
</User>
<User>
<Name>D</Name>
<Id>10</Id>
</User>
<User>
<Name>E</Name>
<Id>11</Id>
</User>
</Users>
(and commented different User for testing purpose) it returned me desired output.