add an element to a node, if it doesnot exist, based on matching criteria - xslt-3.0

Using XSLT 3.0,
I have as input the following XML:
<?xml ="1.0" encoding="UTF-8"?>
<TABLE NAME="TABLE.DB">
<DATA RECORDS="2">
<RECORD ID="1">
<RECNO>1</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<ORDER>10355</ORDER>
<CN>PL</CN>
<PROPERTY>06</PROPERTY>
</RECORD>
<RECORD ID="2">
<RECNO>2</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<ORDER>000026672</ORDER>
<CN>PL 300 L</CN>
</RECORD>
<RECORD ID="3">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10357</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
</RECORD>
</DATA>
</TABLE>
given values used for matching:
(i use \t to define the tab separated nature of my input file)
"10355"\t"PL"
"000026672"\t"PL 300 L"
i need to insert to all records that do not already have a PROPERTY tag, with the value of 06
Desired result:
<?xml ="1.0" encoding="UTF-8"?>
<TABLE NAME="TABLE.DB">
<DATA RECORDS="2">
<RECORD ID="1">
<RECNO>1</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<ORDER>10355</ORDER>
<CN>PL</CN>
<PROPERTY>06</PROPERTY>
</RECORD>
<RECORD ID="2">
<RECNO>2</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<ORDER>000026672</ORDER>
<CN>PL 300 L</CN>
<PROPERTY>06</PROPERTY>
</RECORD>
<RECORD ID="3">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10357</NUMBER>
<CN>PL 300 L</CN>
</RECORD>
</DATA>
</TABLE>
What i have tried, adds the element property, even if it is already there, so i end up with two elements PROPERTY, in the same node, if it already exists. Could you give me an example implementation, i use SAXON latest release (9.8)
xsl: which adds an element, even when one exists:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="xsl exsl xs">
<xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name() = 'RECORD ID']">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<xsl:choose>
<xsl:when test="not(PRODUCT)">
<PRODUCT><xsl:value-of select="98"/></PRODUCT>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:value-of select="98"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Have been using the solution suggested with my real data, (which differ a lot from the example), and i face the following issue:
How could i also have a report that would let me know, which of the additions while they were to be done, according to the input file, were not inserted?

A few observations:
(a) in your sample output, the PROPERTY element has been deleted from record 3. I can't see anything in your description of requirements that explains why.
(b) in your requirements statement, the sentence "i need to insert to all records that do not already have a PROPERTY tag, with the value of 06" is ambiguous. I would read this as saying that if there is a PROPERTY 'tag' (correctly, element) with a value other than 06 then you should insert another PROPERTY element, but that seems to contradict what you say elsewhere.
(c) your code has a template rule with match="//*[local-name() = 'RECORD ID']". You can delete the "//" at the start of a match pattern, it's redundant. More importantly, no element will ever have a local name equal to "RECORD ID" - element names cannot include spaces. So the template rule will never match anything.
(d) assuming that this template rule was intended to match RECORD elements, you certainly don't want the xsl:copy inside the xsl:otherwise, as this will create a nested copy of the whole RECORD.
(e) you've asked for an XSLT 3.0 solution but there's nothing in your problem that requires XSLT 3.0, and in fact your own stylesheet says version="2.0".
(f) I can't see what role the tab-separated parameter file plays in any of this.
In short, there's an awful lot of clarification needed before anyone can start to write any code.

Your solution requires a few changes:
The template shoud match RECORD, not RECORD ID (ID is an
attribute which takes no part in any decision).
Your general concept is OK:
copy the starting tag (<RECORD>),
apply templated to the inside of the current RECORD,
check whether PROPERTY (not PRODUCT) element is absent (in the
current RECORD),
if it is (absent), then output PROPERTY element with the required value,
copy the closing tag (</RECORD>).
I removed the otherwise part (not needed) and changed choose to
a single if..
As you specified XSLT 3.0, even the identity template can be
replaced with (a bit shorter) on-no-match="shallow-copy".
I also added version to your source XML. Otherwise it is not well-formed.
So the whole script can look like below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="RECORD">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<xsl:if test="not(PROPERTY)">
<PROPERTY>06</PROPERTY>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Tested on http://xsltfiddle.liberty-development.net/

Related

How to unique values from different nodes in xslt

I have below xml
input:
<Records count="1">
<Record contentId="2410630" levelId="442" levelGuid="29c1b6a4-b7db-49dc-a703-e78aa1b1246a" moduleId="875" parentId="0">
<Record contentId="2410631" levelId="458" levelGuid="67dbf848-5352-4953-a25b-1b1bbcde89be" moduleId="891" parentId="0">
<Record contentId="2208294" levelId="330" levelGuid="d25cfb04-eb2a-423c-bdab-2db21a58fd4d" moduleId="675" parentId="0">
<Field id="31799" guid="2ebbfd8e-3e89-4be5-9c1f-1b5e85950753" type="1">Unauthorized modification of Information/System - External</Field>
<Field id="31796" guid="24640c19-d1de-415b-b349-25b0af521373" type="6">2208294</Field>
</Record>
</Record>
<Record contentId="2410632" levelId="458" levelGuid="67dbf848-5352-4953-a25b-1b1bbcde89be" moduleId="891" parentId="0">
<Record contentId="2208289" levelId="330" levelGuid="d25cfb04-eb2a-423c-bdab-2db21a58fd4d" moduleId="675" parentId="0">
<Field id="31799" guid="2ebbfd8e-3e89-4be5-9c1f-1b5e85950753" type="1">Inadequate Information Security Practices</Field>
<Field id="31796" guid="24640c19-d1de-415b-b349-25b0af521373" type="6">2208289</Field>
</Record>
</Record>
<Record contentId="2410633" levelId="458" levelGuid="67dbf848-5352-4953-a25b-1b1bbcde89be" moduleId="891" parentId="0">
<Record contentId="2208270" levelId="330" levelGuid="d25cfb04-eb2a-423c-bdab-2db21a58fd4d" moduleId="675" parentId="0">
<Field id="31799" guid="2ebbfd8e-3e89-4be5-9c1f-1b5e85950753" type="1">Loss of Systems Including Data Center</Field>
<Field id="31796" guid="24640c19-d1de-415b-b349-25b0af521373" type="6">2208270</Field>
</Record>
</Record>
<Record contentId="2410636" levelId="458" levelGuid="67dbf848-5352-4953-a25b-1b1bbcde89be" moduleId="891" parentId="0">
<Record contentId="2208289" levelId="330" levelGuid="d25cfb04-eb2a-423c-bdab-2db21a58fd4d" moduleId="675" parentId="0">
<Field id="31799" guid="2ebbfd8e-3e89-4be5-9c1f-1b5e85950753" type="1">Inadequate Information Security Practices</Field>
<Field id="31796" guid="24640c19-d1de-415b-b349-25b0af521373" type="6">2208289</Field>
</Record>
</Record>
<Record contentId="2410661" levelId="463" levelGuid="cc59604e-cc41-4253-879a-5fbde3ffd760" moduleId="896" parentId="0">
<Field id="41541" guid="bae76db7-4e46-4113-a453-68243a76d4f6" type="9">
<Reference id="2208289">Inadequate Information Security Practices</Reference>
</Field>
</Record>
<Record contentId="2410666" levelId="463" levelGuid="cc59604e-cc41-4253-879a-5fbde3ffd760" moduleId="896" parentId="0">
<Field id="41541" guid="bae76db7-4e46-4113-a453-68243a76d4f6" type="9">
<Reference id="2208273"> Loss of 50% Staff </Reference>
</Field>
</Record>
<Record contentId="2410649" levelId="462" levelGuid="83a26d99-e79d-41af-8a20-fa069f791cef" moduleId="895" parentId="0">
<Field id="41453" guid="9a764db7-a75e-4a49-9b26-de03e2bc4bb5" type="9">
<Reference id="2208328">Technology Configuration</Reference>
</Field>
</Record>
</Record>
</Records>
Expected Output:
<uniqueValues>Inadequate Information Security Practices</uniqueValues>
<uniqueValues>Loss of Systems Including Data Center</uniqueValues>
<uniqueValues>Loss of 50% Staff</uniqueValues>
<uniqueValues>Technology Configuration</uniqueValues>
Here is my code
<xsl:stylesheet version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:output method="xml"/>
<xsl:variable name ="fields" select="//Metadata/FieldDefinitions" />
<!--match the root node-->
<xsl:template match="Records">
<ArcherRecords>
<xsl:for-each select="Record[#levelGuid='29c1b6a4-b7db-49dc-a703-e78aa1b1246a']">
<xsl:variable name="valuesTobeCompared" select="Record/Field[#guid='bae76db7-4e46-4113-a453-68243a76d4f6']/Reference/#id"/>
<xsl:for-each-group select="Record/Record" group-by="./Field[#guid='24640c19-d1de-415b-b349-25b0af521373']">
<xsl:choose>
<xsl:when test="$valuesTobeCompared = ./Field[#guid='24640c19-d1de-415b-b349-25b0af521373']">
</xsl:when>
<xsl:otherwise>
<uniqueValues><xsl:value-of select="./Field[#guid='24640c19-d1de-415b-b349-25b0af521373']"/></uniqueValues>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:for-each>
</ArcherRecords>
</xsl:template>
</xsl:stylesheet>
but it is only giving first set of unique values but I want all unique values from entire record set along with it should also have one value of duplicated value, I was not sure how to get all node values into one variable I am able to store only one node values into variable,
Could anybody help me how to write a xslt code to get the unique values
group-by itself will retrieve unique values from provided selectpath, but how can I give multiple node-set values to for-each-group

How can I handle error Exception with try/catch using xslt 3.0

How can I handle error Exception with try/catch using xslt 3.0. I am finding in xml number Element. If number Element not find in parent product then generate a file error.txt and how to write the Exception.
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<product dept="WMN">
<name language="en">Fleece Pullover</name>
<colorChoices>navy black</colorChoices>
</product>
<product dept="ACC">
<number>563</number>
<name language="en">Floppy Sun Hat</name>
</product>
<product dept="ACC">
<number>443</number>
<name language="en">Deluxe Travel Bag</name>
</product>
<product dept="MEN">
<number>784</number>
<name language="en">Cotton Dress Shirt</name>
<colorChoices>white gray</colorChoices>
<desc>Our <i>favorite</i> shirt!</desc>
</product>
</catalog>
Using XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs"
version="3.0">
<xsl:template match="/">
<xsl:stream href="books.xml">
<xsl:iterate select="catalog">
<xsl:result-document href="out.xml" omit-xml-declaration="no" indent="yes">
<xsl:copy>
<xsl:for-each select="product">
<xsl:try>
<xsl:copy-of select="number"/>
<xsl:catch>
<xsl:result-document href="error.txt">
<xsl:message>Element number not given in <xsl:value-of select="product/#dept"/></xsl:message>
<error code="{$err:code}" message="{$err:description}"/>
</xsl:result-document>
</xsl:catch>
</xsl:try>
</xsl:for-each>
</xsl:copy>
</xsl:result-document>
</xsl:iterate>
</xsl:stream>
</xsl:template>
</xsl:stylesheet>
My requirement is how can I uses try/catch when not find the number element then make a file with error.
Try this to generate error message in log file by taking the value in variable in then check some conditon:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs"
version="3.0">
<xsl:template match="/">
<xsl:stream href="books.xml">
<xsl:iterate select="catalog">
<xsl:result-document href="out.xml" omit-xml-declaration="no" indent="yes">
<xsl:copy>
<xsl:for-each select="product">
<xsl:variable name="num" select="if (number) then (number) else ('aaaa')"/>
<xsl:try>
<number>
<xsl:value-of select="format-number($num, '############')"/>
</number>
<xsl:catch>
<xsl:result-document href="error.log" omit-xml-declaration="yes">
<xsl:value-of select="concat(' Element number not given in ', #dept)"/>
<xsl:value-of select="concat(' Error code: ', $err:code, ' and Error Desc is: ', $err:description)"/>
</xsl:result-document>
</xsl:catch>
</xsl:try>
</xsl:for-each>
</xsl:copy>
</xsl:result-document>
</xsl:iterate>
</xsl:stream>
</xsl:template>
</xsl:stylesheet>

append data ton an element, based on matching this exact element

Given the following XML as input,
<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="TABLE.DB">
<DATA RECORDS="2">
<RECORD ID="4">
<RECNO>0</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10354</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
<RECORD ID="3">
<RECNO>1</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10355</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
<RECORD ID="2">
<RECNO>2</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10356</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<SUB_A>Some random data not matched.</SUB_A>
</RECORD>
<RECORD ID="1">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10357</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>19837</TOTALS>
</RECORD>
<RECORD ID="0">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10358</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
</RECORD>
</DATA>
</TABLE>
and the following tab separated file:
[yet another value.]\t10358
value i'd like to add\t10355
(another) value i'd like to add\t10357
i used \t, in order to show where the tab exists in the file. i would like to append the data found in the first column, into the element i try to match on, which in this case in NUMBER.
So if NUMBER equals second column, append to it the value found in the first column, using | as a separator.
how one could have the below result, but with keeping the order of the records, sorting on the element Output:
<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="TABLE.DB">
<DATA RECORDS="2">
<RECORD ID="0">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10358 | [yet another value.]</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
</RECORD>
<RECORD ID="1">
<RECNO>3</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10357 | (another) value i'd like to add</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>19837</TOTALS>
</RECORD>
<RECORD ID="2">
<RECNO>2</RECNO>
<SEQUENCE>0</SEQUENCE>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10356</NUMBER>
<CN>PL 300 L</CN>
<PROPERTY>0</PROPERTY>
<DAYS>10</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<SUB_A>Some random data not matched.</SUB_A>
</RECORD>
<RECORD ID="3">
<RECNO>1</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10355 | value i'd like to add</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
<RECORD ID="4">
<RECNO>0</RECNO>
<SEQ>0</SEQ>
<DATE>17/12/1999 2:44:08 μμ</DATE>
<ID>12/11/2015 3:15:25 μμ</ID>
<NUMBER>10354</NUMBER>
<CN>PL</CN>
<PROPERTY>0</PROPERTY>
<DAYS>0</DAYS>
<CURRENTSTATUS>0</CURRENTSTATUS>
<TOTALS>1</TOTALS>
</RECORD>
</DATA>
</TABLE>
I use Saxon latest, v 9.8
The xsl:merge is similar to your previous question and to sort the merge result you can simply wrap the xsl:merge into an xsl:perform-sort:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
expand-text="yes" version="3.0">
<xsl:param name="text-uri" as="xs:string">test2017100602.txt</xsl:param>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="lines" as="element(line)*">
<xsl:apply-templates select="unparsed-text-lines($text-uri)"/>
</xsl:variable>
<xsl:template match=".[. instance of xs:string]">
<xsl:variable name="tokens" as="xs:string*" select="tokenize(., ' ')[normalize-space()]"/>
<line number="{$tokens[2]}">{$tokens[1]}</line>
</xsl:template>
<xsl:template match="TABLE/DATA">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:perform-sort>
<xsl:sort select="xs:integer(#ID)"/>
<xsl:merge>
<xsl:merge-source name="record" select="RECORD">
<xsl:merge-key select="NUMBER"/>
</xsl:merge-source>
<xsl:merge-source name="line" select="$lines" sort-before-merge="yes">
<xsl:merge-key select="#number"/>
</xsl:merge-source>
<xsl:merge-action>
<xsl:if test="current-merge-group('record')">
<xsl:copy>
<xsl:apply-templates select="#*, NUMBER/preceding-sibling::*"/>
<NUMBER>
<xsl:value-of select="NUMBER, current-merge-group()[2]"
separator=" | "/>
</NUMBER>
<xsl:apply-templates select="NUMBER/following-sibling::*"/>
</xsl:copy>
</xsl:if>
</xsl:merge-action>
</xsl:merge>
</xsl:perform-sort>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

String replace and concatenation

I have this structure
<ROWS>
<ROW>
<TEXT> This is a #good# #day# </TEXT>
<good>great</good>
<day>month</day>
</ROW>
<ROW>
<TEXT> This is a #good# #day# </TEXT>
<good>Fun</good>
<day>morning</day>
</ROW>
</ROWS>
How do I change that to
<statement> This is a great month, this is a Fun morning </statement>
Using only XSLT 1.0?
The original XML can change tag name. But not the structure! Any ideas?
This seems somewhat similar to creating form letters from a template. Assuming the example is not to be meant literally, you could try something like:
<?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="/">
<statements>
<xsl:for-each select="ROWS/ROW/TEXT">
<statement>
<xsl:call-template name="merge">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</statement>
</xsl:for-each>
</statements>
</xsl:template>
<xsl:template name="merge">
<xsl:param name="string"/>
<xsl:param name="sep" select="'#'"/>
<xsl:choose>
<xsl:when test="contains($string, $sep) and contains(substring-after($string, $sep), $sep)">
<xsl:value-of select="substring-before($string, $sep)" />
<xsl:variable name="placeholder" select="substring-before(substring-after($string, $sep), $sep)" />
<xsl:value-of select="../*[name() = $placeholder]" />
<!-- recursive call -->
<xsl:call-template name="merge">
<xsl:with-param name="string" select="substring-after(substring-after($string, $sep), $sep)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Given an input of:
<ROWS>
<ROW>
<TEXT>The quick brown #animal# jumps over the #property# dog.</TEXT>
<animal>fox</animal>
<property>lazy</property>
</ROW>
<ROW>
<TEXT>A journey of a #number# miles #action# with a single #act#.</TEXT>
<number>thousand</number>
<action>begins</action>
<act>step</act>
</ROW>
</ROWS>
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<statements>
<statement>The quick brown fox jumps over the lazy dog.</statement>
<statement>A journey of a thousand miles begins with a single step.</statement>
</statements>
Look at the XSLT 1.0 spec; find the section on string functions. Study the contains, substring-before, and substring-after functions. The solution to your problem should become clear; if it doesn't, you should at least be able to get far enough on your problem to pose a question that does not look as if it could be paraphrased as "Please do my homework for me."

XSLT: replace an integer by a string

I have a little problem.
A node in my XML may contains and integer, and i have to replace this integer by a string.
Each number match with a string.
For example i have:
Integer - String
1 - TODO
2 - IN PROGRESS
3 - DONE
4 - ERROR
5 - ABORTED
Original XML:
<root>
<status>1</status>
</root>
Converted XML:
<root>
<status>TODO</status>
</root>
So i want replace 1 by "TODO", 2 by "IN PROGRESS" ...
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root/status">
<root>
<status>
<xsl:variable name="text" select="." />
<xsl:choose>
<xsl:when test="contains($text, '1')">
<xsl:value-of select="'TODO'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</status></root>
</xsl:template>
</xsl:stylesheet>
I'am asking if there is another way to do that.
There are a number of ways of doing this. Where the translation is from consecutive integers in the range 1 to N, I would use
<xsl:variable name="index" select="xs:integer(status)"/>
<xsl:value-of select="('TODO', 'IN PROGRESS', 'DONE', 'ERROR', 'ABORTED')[$index]"/>
In other cases where there's a small number of values I might use template rules:
<xsl:template match="status[.='1']" mode="lookup">TODO</xsl:template>
<xsl:template match="status[.='2']" mode="lookup">IN PROGRESS</xsl:template>
etc.
In other cases a lookup table makes sense (note that Dimitre's version with its cumbersome document('') call is designed for XSLT 1.0 - it's considerably simpler if you're using 2.0. When people don't say what version they are using I generally assume 2.0 and Dimitre generally assumes 1.0.)
I'm increasingly seeing people make the mistake of using contains() when they mean "=". If you want to test whether the content of a node is "X", use $node = "X", not contains($node, "X").
One way to do this, is to create a sort of 'look-up' table of values. This could be embedded in the XSLT, or put in a separate file. For example, if you put it in the XSLT file, it would look something like this..
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lookup="lookup">
<lookup:data>
<status code="1">TO DO</status>
<status code="2">IN PROGRESS</status>
<status code="3">DONE</status>
</lookup:data>
Then, you would also create a variable to access this data
<xsl:variable name="lookup" select="document('')/*/lookup:data"/>
Finally, to look up the value, you would simply do this
<xsl:value-of select="$lookup/status[#code = '1']/>
Here is the full XSLT in this case
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lookup="lookup">
<xsl:output method="xml" indent="yes"/>
<lookup:data>
<status code="1">TO DO</status>
<status code="2">IN PROGRESS</status>
<status code="3">DONE</status>
</lookup:data>
<xsl:variable name="lookup" select="document('')/*/lookup:data"/>
<xsl:template match="status/text()">
<xsl:value-of select="$lookup/status[#code = current()]" />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your sample XML, the following is output
<root>
<status>TODO</status>
</root>
It could be better to have these in a separate file though, as then they can be re-used in other stylesheets. To do this, just create a file, called 'lookup.xml', and add the XML
<data>
<status code="1">TO DO</status>
<status code="2">IN PROGRESS</status>
<status code="3">DONE</status>
</data>
Note, you don't need namespaces in this case. Then just change the definition of the variable to the following
<xsl:variable name="lookup" select="document('lookup.xml')/data"/>
You have lots of unnecessary code in your solution. The following is a simplified version which works the same way:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root/status">
<root>
<status>
<xsl:choose>
<xsl:when test="contains(.,'1')">TODO</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</status>
</root>
</xsl:template>
</xsl:stylesheet>
The simplest approach is to start with the identity transform and then add special cases:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="status[. = '1']">
<status>TODO</status>
</xsl:template>
<!-- likewise for status[. = '2'] etc. -->
<!-- copy everything else -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With XSLT Version 3.0 you can use a map type:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="MyMap" select="
map {
'1' : 'TODO',
'2' : 'IN PROGRESS',
'3' : 'DONE',
'4' : 'ERROR',
'5' : 'ABORTED'}">
</xsl:variable>
<xsl:template match="/root/status">
<status>
<xsl:variable name="text" select="."/>
<xsl:value-of select="$MyMap( $text )"/>
</status>
</xsl:template>
</xsl:stylesheet>
My word. XSLT is not easy and I don't think it should be made any harder than need be by showing off your knowledge of the inner workings as shown in some of the other answers.
For ease you've hit the nail on the head, use a Choose statement. I'd probably pull it out into a separate templates (I uses these like methods in other languages) simply to ease testing and help clean up your code a little for ease of reading.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="PrintStatus">
<!-- param we can pass a value into or default to the current node -->
<xsl:param name="text" select="." />
<xsl:choose>
<xsl:when test="contains($text, '1')">
<xsl:value-of select="'TODO'"/>
</xsl:when>
<!-- Assume your others go here -->
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/root/status">
<root>
<status>
<xsl:call-template name="PrintStatus" />
</status>
</root>
</xsl:template>
</xsl:stylesheet>
Keep it simple unless you need the extra complications.
A trick I sometimes use in cases like this is to use a list of values in one string, and take a substring, like this:
<xsl:variable name="statuslist">TODO IN PROGRESSDONE ERROR ABORTED </xsl:variable>
<xsl:template match="status/text()">
<xsl:value-of select="normalize-space(substring($statuslist, ( . - 1 ) * 11 , 11))" />
</xsl:template>
Note, the values in the 'statuslist' are exactly 11 characters apart (the length of your longest value), hence the * 11 and ,11 in your substring. Because you count from 1 not 0, you have to subtract 1 from your index. Alternatively you could pad the variable with 11 spaces at the beginning rather than subtract 1, it's up to you. The normalize-space call just strips the excess spaces from the extracted value.
If you want to make it neater, you could put a separator between each value, and use *12,11 in that substring call instead.
It's not a solution that scales well if you have a large number of possible values, and obviously it needs your possible ids to be in a sequence, but if there's only a few values it's fairly compact.

Resources