XSLT 1.0 Json Array [duplicate] - xslt-1.0

How would you convert JSON to XML?
Consider:
<sampleTag>
{
"Order": {
"InvestmentAccount": { "AccountNumber": "10" },
"Parcel": {
"Limit": "0",
"ExpiryDate": "1900-01-01T00:00:00",
"Asset": [
{
"Open": "25.15",
"High": "25.15",
"Low": "25.11",
"Close": "25.87"
}
{
"Open": "25.15",
"High": "25.15",
"Low": "25.11",
"Close": "25.87"
}]
},
"OrderDate": "2012-10-11T21:46:03.6489906+11:00",
}
}
</sampleTag>
After transformation, the document is as follows:
<Order>
<InvestmentAccount>
<AccountNumber>10</AccountNumber>
</InvestmentAccount>
<Parcel>
<Limit>0</Limit>
<ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
<Asset>
<Open>25.15</Open>
<High>25.15</High>
<Low>25.11</Low>
<Close>25.87</Close>
</Asset>
<Asset>
<Open>25.15</Open>
<High>25.15</High>
<Low>25.11</Low>
<Close>25.87</Close>
</Asset>
</Parcel>
<OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

My work on JSON parsing doesn't cover the full JSON grammar.
And the task of "translating" any JSON document to an XML document doesn't have a solution. There are JSON constructs, which cannot be translated to XML without defining additional conventions and introducing additional elements -- so the final XML structure isn't a true and natural representation of the original JSON object.
In XSLT 3.0 there is a function to parse any JSON object -- parse-json() -- to a map -- a new data type introduced in XSLT 3.0. Read about this here:
http://www.w3.org/TR/xslt-30/#json

Actually, it is not that hard. The way to approach it is to examine the syntax of jason, and view each production like it was a template. I was just about to write a solution, when I considered the possibility that the OP forgot to google for pre-existing solutions. I searched and lo and behold ....
http://dnovatchev.wordpress.com/2007/07/05/transforming-json/
UPDATE
Here is a JSon to XML converter. But it only works on a subset of json. Hopefully, the subset is broad enough for your particular needs. In particular the limitations are:
The only simple type supported is string. No integer, boolean or null.
Json object names must be valid xml element names.
No escape codes permitted inside string values. This means that you cant transport values that include, for instance, the " character (without rolling your own encoding layer).
This XSLT 1.0 style-sheet...*
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:exsl="http://exslt.org/common"
xmlns:so="http://stackoverflow.com/questions/13007280"
exclude-result-prefixes="xsl xs json so exsl">
<xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:variable name="quot" select="'"'" />
<xsl:template match="/*">
<xsl:variable name="t1">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />
</xsl:template>
<xsl:template match="*" mode="copy-sans-namespace">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="copy-sans-namespace" />
</xsl:element>
</xsl:template>
<xsl:template name="field">
<!-- Input like: "Open": "25.15" bla -->
<!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
<xsl:variable name="remainder" select="substring-after($json-in,':')" />
<xsl:call-template name="value">
<xsl:with-param name="json-in" select="$remainder" />
<xsl:with-param name="parent-ele" select="$field-name" />
</xsl:call-template>
</xsl:template>
<xsl:template name="fields">
<!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
<!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:variable name="n" select="normalize-space($json-in)" />
<xsl:choose>
<xsl:when test="substring($n,1,1) = $quot">
<xsl:variable name="t1">
<xsl:call-template name="field">
<xsl:with-param name="json-in" select="$n" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="substring($t2,1,1)=','">
<xsl:call-template name="fields">
<xsl:with-param name="json-in" select="substring-after($t2,',')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$t2">
<so:extra><xsl:value-of select="$t2" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
</so:output>
<xsl:copy-of select="exsl:node-set($t3)/so:extra" />
</xsl:when>
<xsl:when test="$n">
<so:extra><xsl:value-of select="$n" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="object">
<!-- Input like: { X } bla -->
<!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" select="''" />
<xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
<xsl:variable name="t2">
<xsl:call-template name="fields">
<xsl:with-param name="json-in" select="$t1" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
<so:output>
<xsl:choose>
<xsl:when test="$parent-ele">
<xsl:element name="{$parent-ele}">
<xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
</xsl:otherwise>
</xsl:choose>
</so:output>
<xsl:if test="$t3">
<so:extra><xsl:value-of select="$t3" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="objects">
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="n" select="normalize-space($json-in)" />
<xsl:choose>
<xsl:when test="substring($n,1,1) = '{'">
<xsl:variable name="t1">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="$n" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="substring($t2,1,1)='{'">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="$t2" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$t2">
<so:extra><xsl:value-of select="$t2" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
</so:output>
<xsl:copy-of select="exsl:node-set($t3)/so:extra" />
</xsl:when>
<xsl:when test="$n">
<so:extra><xsl:value-of select="$n" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="array">
<!-- Input like: [ X1 X2 ] bla -->
<!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
<xsl:variable name="t2">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="$t1" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, ']'))" />
<xsl:copy-of select="exsl:node-set($t2)/so:output" />
<xsl:if test="$t3">
<so:extra><xsl:value-of select="$t3" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="value">
<!-- Input like either array, object or string -->
<!-- output like either array, object or string -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
<xsl:choose>
<xsl:when test="$first-letter='{'">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$first-letter='['">
<xsl:call-template name="array">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$first-letter=$quot">
<xsl:call-template name="string">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<so:output>ERROR</so:output>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="string">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
<xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
...applied to this input (modified from OP supplied to remove an extraneous comma)...
<sampleTag>
{
"Order": {
"InvestmentAccount": { "AccountNumber": "10" },
"Parcel": {
"Limit": "0",
"ExpiryDate": "1900-01-01T00:00:00",
"Asset": [
{
"Open": "25.15",
"High": "25.15",
"Low": "25.11",
"Close": "25.87"
}
{
"Open": "25.15",
"High": "25.15",
"Low": "25.11",
"Close": "25.87"
}]
},
"OrderDate": "2012-10-11T21:46:03.6489906+11:00"
}
}
</sampleTag>
..yields...
<Order>
<InvestmentAccount>
<AccountNumber>10</AccountNumber>
</InvestmentAccount>
<Parcel>
<Limit>0</Limit>
<ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
<Asset>
<Open>25.15</Open>
<High>25.15</High>
<Low>25.11</Low>
<Close>25.87</Close>
</Asset>
<Asset>
<Open>25.15</Open>
<High>25.15</High>
<Low>25.11</Low>
<Close>25.87</Close>
</Asset>
</Parcel>
<OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

I tweaked Sean B. Durkin's template a bit and thought I'd share.
Updates include:
Support for numbers
Support for booleans
Fix for object array elements separated by a comma (per JSON spec)
Non-update changes:
Array elements are displayed in their own XML elements with the element name as the object key suffixed with _element
Still not supported:
Escaped characters (quotes) in strings
Here's the template:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:exsl="http://exslt.org/common"
xmlns:so="http://stackoverflow.com/questions/13007280"
exclude-result-prefixes="xsl xs json so exsl">
<xsl:output indent="yes" encoding="UTF-8" />
<xsl:strip-space elements="*" />
<xsl:variable name="quot" select="'"'" />
<xsl:variable name="numbers" select="'0123456789'"/>
<xsl:variable name="booleans" select="'tf'"/>
<xsl:template match="/*">
<xsl:variable name="t1">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />
</xsl:template>
<xsl:template match="*" mode="copy-sans-namespace">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="copy-sans-namespace" />
</xsl:element>
</xsl:template>
<xsl:template name="field">
<!-- Input like: "Open": "25.15" bla -->
<!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
<xsl:variable name="remainder" select="substring-after($json-in,':')" />
<xsl:call-template name="value">
<xsl:with-param name="json-in" select="$remainder" />
<xsl:with-param name="parent-ele" select="$field-name" />
</xsl:call-template>
</xsl:template>
<xsl:template name="fields">
<!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
<!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:variable name="n" select="normalize-space($json-in)" />
<xsl:choose>
<xsl:when test="substring($n,1,1) = $quot">
<xsl:variable name="t1">
<xsl:call-template name="field">
<xsl:with-param name="json-in" select="$n" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="substring($t2,1,1)=','">
<xsl:call-template name="fields">
<xsl:with-param name="json-in" select="substring-after($t2,',')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$t2">
<so:extra><xsl:value-of select="$t2" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
</so:output>
<xsl:copy-of select="exsl:node-set($t3)/so:extra" />
</xsl:when>
<xsl:when test="$n">
<so:extra><xsl:value-of select="$n" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="object">
<!-- Input like: { X } bla -->
<!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" select="''" />
<xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
<xsl:variable name="t2">
<xsl:call-template name="fields">
<xsl:with-param name="json-in" select="$t1" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
<so:output>
<xsl:choose>
<xsl:when test="$parent-ele">
<xsl:element name="{$parent-ele}">
<xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
</xsl:otherwise>
</xsl:choose>
</so:output>
<xsl:if test="$t3">
<so:extra><xsl:value-of select="$t3" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="objects">
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="n" select="normalize-space($json-in)" />
<xsl:choose>
<xsl:when test="substring($n,1,1) = '{'">
<xsl:variable name="t1">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="$n" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="substring($t2,1,1)='{'">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="$t2" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="substring($t2,1,1)=',' and substring(normalize-space(substring-after($t2,',')),1,1)='{'">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="normalize-space(substring-after($t2,','))" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$t2">
<so:extra><xsl:value-of select="$t2" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
</so:output>
<xsl:copy-of select="exsl:node-set($t3)/so:extra" />
</xsl:when>
<xsl:when test="$n">
<so:extra><xsl:value-of select="$n" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="array">
<!-- Input like: [ X1 X2 ] bla -->
<!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
<xsl:variable name="t2">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="$t1" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="contains(substring-before(exsl:node-set($t2)/so:extra,']'),',')">
<xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,','))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,']'))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="t4">
<xsl:element name="{$parent-ele}">
<xsl:for-each select="$t2/so:output/*[local-name(.)=$parent-ele]">
<xsl:variable name="self" select="."/>
<xsl:variable name="tempResult">
<xsl:element name="{concat($parent-ele,'_element')}">
<xsl:copy-of select="exsl:node-set($self/*)" />
</xsl:element>
</xsl:variable>
<xsl:copy-of select="exsl:node-set($tempResult)"/>
</xsl:for-each>
</xsl:element>
</xsl:variable>
<xsl:variable name="t5" select="exsl:node-set($t4)"/>
<so:output>
<xsl:copy-of select="$t5"/>
</so:output>
<xsl:if test="$t3">
<so:extra><xsl:value-of select="$t3" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="value">
<!-- Input like either array, object or string -->
<!-- output like either array, object or string -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
<xsl:choose>
<xsl:when test="$first-letter='{'">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$first-letter='['">
<xsl:call-template name="array">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$first-letter=$quot">
<xsl:call-template name="string">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($numbers,$first-letter)">
<xsl:call-template name="number">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($booleans,$first-letter)">
<xsl:call-template name="boolean">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<so:output>ERROR</so:output>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="string">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
<xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="number">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="remainder">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="boolean">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="remainder">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
So with this (adjusted) input:
<?xml version="1.0" encoding="UTF-8"?>
<sampleTag><![CDATA[
{
"Order": {
"InvestmentAccount": { "AccountNumber": "10" },
"Parcel": {
"Limit": 0,
"ExpiryDate": "1900-01-01T00:00:00",
"valid": true,
"Asset": [
{
"Open": 25.15,
"High": 25.15,
"Low": 25.11,
"Close": 25.87
},
{
"Open": 25.15,
"High": 25.15,
"Low": 25.11,
"Close": 25.87
}
]
},
"OrderDate": "2012-10-11T21:46:03.6489906+11:00"
}
}
]]></sampleTag>
I get this output:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<InvestmentAccount>
<AccountNumber>10</AccountNumber>
</InvestmentAccount>
<Parcel>
<Limit>0</Limit>
<ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
<valid>true</valid>
<Asset>
<Asset_element>
<Open>25.15</Open>
<High>25.15</High>
<Low>25.11</Low>
<Close>25.87</Close>
</Asset_element>
<Asset_element>
<Open>25.15</Open>
<High>25.15</High>
<Low>25.11</Low>
<Close>25.87</Close>
</Asset_element>
</Asset>
</Parcel>
<OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

Updating Samuel Murphy answer.
Updates include:
Support for null
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
xmlns:exsl="http://exslt.org/common"
xmlns:so="http://stackoverflow.com/questions/13007280"
exclude-result-prefixes="xsl xs json so exsl">
<xsl:output indent="yes" encoding="UTF-8" />
<xsl:strip-space elements="*" />
<xsl:variable name="quot" select="'"'" />
<xsl:variable name="numbers" select="'0123456789'"/>
<xsl:variable name="booleans" select="'tf'"/>
<xsl:variable name="nulls" select="'n'"/>
<xsl:template match="/*">
<xsl:variable name="t1">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />
</xsl:template>
<xsl:template match="*" mode="copy-sans-namespace">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="copy-sans-namespace" />
</xsl:element>
</xsl:template>
<xsl:template name="field">
<!-- Input like: "Open": "25.15" bla -->
<!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
<xsl:variable name="remainder" select="substring-after($json-in,':')" />
<xsl:call-template name="value">
<xsl:with-param name="json-in" select="$remainder" />
<xsl:with-param name="parent-ele" select="$field-name" />
</xsl:call-template>
</xsl:template>
<xsl:template name="fields">
<!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
<!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:variable name="n" select="normalize-space($json-in)" />
<xsl:choose>
<xsl:when test="substring($n,1,1) = $quot">
<xsl:variable name="t1">
<xsl:call-template name="field">
<xsl:with-param name="json-in" select="$n" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="substring($t2,1,1)=','">
<xsl:call-template name="fields">
<xsl:with-param name="json-in" select="substring-after($t2,',')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$t2">
<so:extra><xsl:value-of select="$t2" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
</so:output>
<xsl:copy-of select="exsl:node-set($t3)/so:extra" />
</xsl:when>
<xsl:when test="$n">
<so:extra><xsl:value-of select="$n" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="object">
<!-- Input like: { X } bla -->
<!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" select="''" />
<xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
<xsl:variable name="t2">
<xsl:call-template name="fields">
<xsl:with-param name="json-in" select="$t1" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
<so:output>
<xsl:choose>
<xsl:when test="$parent-ele">
<xsl:element name="{$parent-ele}">
<xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
</xsl:otherwise>
</xsl:choose>
</so:output>
<xsl:if test="$t3">
<so:extra><xsl:value-of select="$t3" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="objects">
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="n" select="normalize-space($json-in)" />
<xsl:choose>
<xsl:when test="substring($n,1,1) = '{'">
<xsl:variable name="t1">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="$n" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="substring($t2,1,1)='{'">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="$t2" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="substring($t2,1,1)=',' and substring(normalize-space(substring-after($t2,',')),1,1)='{'">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="normalize-space(substring-after($t2,','))" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$t2">
<so:extra><xsl:value-of select="$t2" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
</so:output>
<xsl:copy-of select="exsl:node-set($t3)/so:extra" />
</xsl:when>
<xsl:when test="$n">
<so:extra><xsl:value-of select="$n" /></so:extra>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="array">
<!-- Input like: [ X1 X2 ] bla -->
<!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
<xsl:variable name="t2">
<xsl:call-template name="objects">
<xsl:with-param name="json-in" select="$t1" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t3">
<xsl:choose>
<xsl:when test="contains(substring-before(exsl:node-set($t2)/so:extra,']'),',')">
<xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,','))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,']'))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="t4">
<xsl:element name="{$parent-ele}">
<xsl:for-each select="$t2/so:output/*[local-name(.)=$parent-ele]">
<xsl:variable name="self" select="."/>
<xsl:variable name="tempResult">
<xsl:element name="{concat($parent-ele,'_element')}">
<xsl:copy-of select="exsl:node-set($self/*)" />
</xsl:element>
</xsl:variable>
<xsl:copy-of select="exsl:node-set($tempResult)"/>
</xsl:for-each>
</xsl:element>
</xsl:variable>
<xsl:variable name="t5" select="exsl:node-set($t4)"/>
<so:output>
<xsl:copy-of select="$t5"/>
</so:output>
<xsl:if test="$t3">
<so:extra><xsl:value-of select="$t3" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="value">
<!-- Input like either array, object or string -->
<!-- output like either array, object or string -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
<xsl:choose>
<xsl:when test="$first-letter='{'">
<xsl:call-template name="object">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$first-letter='['">
<xsl:call-template name="array">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$first-letter=$quot">
<xsl:call-template name="string">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($numbers,$first-letter)">
<xsl:call-template name="number">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($booleans,$first-letter)">
<xsl:call-template name="boolean">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($nulls,$first-letter)">
<xsl:call-template name="boolean">
<xsl:with-param name="json-in" select="$json-in" />
<xsl:with-param name="parent-ele" select="$parent-ele"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<so:output>ERROR</so:output>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="string">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
<xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="number">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="remainder">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
<xsl:template name="boolean">
<!-- Input like: "X" bla -->
<!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
<xsl:param name="json-in" />
<xsl:param name="parent-ele" />
<xsl:variable name="value">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="remainder">
<xsl:choose>
<xsl:when test="contains(substring-before($json-in,','),'}')">
<xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
</xsl:when>
<xsl:when test="contains(substring-before($json-in,','),']')">
<xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<so:output>
<xsl:element name="{$parent-ele}">
<xsl:value-of select="$value" />
</xsl:element>
</so:output>
<xsl:if test="$remainder">
<so:extra><xsl:value-of select="$remainder" /></so:extra>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

The question is tagged for XSLT 1.0, so I do not know if this answer will help the original question. But if you can use XSLT 3.0, the function json-to-xml does exactly what you need.
https://www.w3.org/TR/xslt-30/#func-json-to-xml

As well as Dimitre's XSLT parsing framework, there is also Gunther Rademacher's Rex parser generator, which also includes JSON as one of its sample grammars:
http://www.bottlecaps.de/rex/

Try this lib:
https://github.com/bramstein/xsltjson
Looks very nice.
Its a 2.0 XSLT solution, although he also points to 1.0 version.

XSLT has many strengths and a few big weaknesses. Text processing is its weakness at least version 1.0
Although it would be technically possible to process that text with XSLT 1.0, I can't think of any situation where it would be a very good idea, and where it wouldn't be a very fragile conversion. The code you would have to produce would be very unwieldy.
Is there no other language available to you to do the processing?

Related

Template Name and call-template

i have xslt for change status transaction. i can change for template who was in scope at template name bodys. but the plan was when data on headers have failed some condition and the status for the template bodys in node TxRsnSts need change too. but i cant get the value from headers. how i can get the value from headers and submit on variable name TxRsnStsB.
This the XSLT script i make
<xsl:stylesheet version="1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="urn:iso:std:iso:20022:tech:xsd:head.001.001.01" xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:pacs.002.001.10" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<ns:BusMsg xmlns:ns="urn:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso ../../../xsd/phase1/MainCIHub.xsd">
<ns1:AppHdr>
<xsl:call-template name="headers">
<xsl:with-param name="CpyDplct" select="//BusMsg/AppHdr/CpyDplct"/>
<xsl:with-param name="PssblDplct" select="//BusMsg/AppHdr/PssblDplct"/>
<xsl:with-param name="Sgntr" select="//BusMsg/AppHdr/Sgntr"/>
</xsl:call-template>
</ns1:AppHdr>
<ns:Document>
<ns:FIToFIPmtStsRpt>
<xsl:call-template name="bodys">
<xsl:with-param name="CtgyPurpB" select="//BusMsg/Document/FItoFICstmrCdtTrf/CdtTrfTxInf/PmtTpInf/CtgyPurp/Prtry"/>
<xsl:with-param name="IntrBkSttlmAmtB" select="//BusMsg/Document/FItoFICstmrCdtTrf/CdtTrfTxInf/IntrBkSttlmAmt/Value"/>
<xsl:with-param name="CcyB" select="//BusMsg/Document/FItoFICstmrCdtTrf/CdtTrfTxInf/IntrBkSttlmAmt/Ccy"/>
</xsl:call-template>
</ns:FIToFIPmtStsRpt>
</ns:Document>
</ns:BusMsg>
</xsl:template>
<xsl:template name="headers">
<xsl:param name="CpyDplct"/>
<xsl:param name="PssblDplct"/>
<xsl:param name="Sgntr"/>
<xsl:variable name="flagCpyDplcts">
<xsl:choose>
<xsl:when test="$CpyDplct = ''"/>
<xsl:when test="string-length($CpyDplct) <= 35 and ($CpyDplct = 'CODU' or $CpyDplct = 'COPY' or $CpyDplct = 'DUPL')">
<xsl:value-of select="$CpyDplct"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="$CpyDplct"/>
</xsl:copy>
<xsl:value-of select="'U0002'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagPssblDplcts">
<xsl:choose>
<xsl:when test="$PssblDplct = ''"/>
<xsl:when test="string-length($PssblDplct) <= 35 and ($PssblDplct = 'true' or $PssblDplct = 'false')">
<xsl:value-of select="$PssblDplct"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="$PssblDplct"/>
</xsl:copy>
<xsl:value-of select="'U0002'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagSgntrs">
<xsl:choose>
<xsl:when test="$Sgntr = ''"/>
<xsl:when test="string-length($Sgntr) <= 35">
<xsl:value-of select="$Sgntr"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="$PssblDplct"/>
</xsl:copy>
<xsl:value-of select="'U0012'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ns1:CpyDplct>
<xsl:value-of select="$flagCpyDplcts"/>
</ns1:CpyDplct>
<ns1:PssblDplct>
<xsl:value-of select="$flagPssblDplcts"/>
</ns1:PssblDplct>
<ns1:Sgntr>
<xsl:value-of select="$flagSgntrs"/>
</ns1:Sgntr>
</xsl:template>
<xsl:template name="bodys">
<xsl:param name="CtgyPurpB"/>
<xsl:param name="IntrBkSttlmAmtB"/>
<xsl:param name="CcyB"/>
<xsl:variable name="flagCtgyPurpBs">
<xsl:variable name="payT" select="substring($CtgyPurpB,1,3)"/>
<xsl:variable name="catP" select="substring($CtgyPurpB,4,2)"/>
<xsl:choose>
<xsl:when test="not(contains($payT,'010') or contains($payT,'011') or contains($payT,'019') or contains($payT,'110') or contains($payT,'510') or contains($payT,'610') or contains($payT,'710') or contains($payT,'720') or contains($payT,'000'))">
<xsl:value-of select="'U0002'"/>
</xsl:when>
<xsl:when test="not(contains($catP,'01') or contains($catP,'02') or contains($catP,'03') or contains($catP,'99'))">
<xsl:value-of select="'U0002'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$CtgyPurpB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagIntrBkSttlmAmtBs">
<xsl:variable name="amount" select="substring-before($IntrBkSttlmAmtB,'.')"/>
<xsl:variable name="decimal" select="substring-after($IntrBkSttlmAmtB,'.')"/>
<xsl:choose>
<xsl:when test="not(fn:matches($IntrBkSttlmAmtB, '^[0-9.]*$'))">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:when test="not(string-length($amount) <= 16)">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:when test="not(string-length($decimal) <= 2)">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$IntrBkSttlmAmtB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagCcyBs">
<xsl:choose>
<xsl:when test="not(fn:matches($CcyB, '^[a-zA-Z0-9]*$') and string-length($CcyB) <= 3)">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$CcyB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ns1:CtgyPurp>
<xsl:value-of select="$flagCtgyPurpBs"/>
</ns1:CtgyPurp>
<ns1:IntrBkSttlmAmt>
<xsl:value-of select="$flagIntrBkSttlmAmtBs"/>
</ns1:IntrBkSttlmAmt>
<ns1:Ccy>
<xsl:value-of select="$flagCcyBs"/>
</ns1:Ccy>
<xsl:variable name="TxRsnStsB">
<xsl:choose>
<xsl:when
test="contains($flagSgntrs, 'U0012') or contains($flagSgntrs, 'U0002') or contains($flagSgntrs, 'U0038') or contains($flagPssblDplcts, 'U0012') or contains($flagPssblDplcts, 'U0002') or contains($flagPssblDplcts, 'U0038') or contains($flagCpyDplcts, 'U0012') or contains($flagCpyDplcts, 'U0002') or contains($flagCpyDplcts, 'U0038') or contains($flagCtgyPurpBs, 'U0012') or contains($flagCtgyPurpBs, 'U0002') or contains($flagCtgyPurpBs, 'U0038') or contains($flagIntrBkSttlmAmtBs, 'U0012') or contains($flagIntrBkSttlmAmtBs, 'U0002') or contains($flagIntrBkSttlmAmtBs, 'U0038') or contains($flagCcyBs, 'U0012') or contains($flagCcyBs, 'U0002') or contains($flagCcyBs, 'U0038')">
<xsl:value-of select="'Decline'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'Approve'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ns1:TxRsnSts><xsl:value-of select="$TxRsnStsB"/></ns1:TxRsnSts>
</xsl:template>
</xsl:stylesheet>
Your question is hard to understand.
If you want to reuse the variabels you have declared in headers inside body, you have two options:
Save the result of headers inside a variable and use that as a parameter in your call to bodys.
The other option is to declare those variables as globals like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:ns1="urn:iso:std:iso:20022:tech:xsd:head.001.001.01"
xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:pacs.002.001.10"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="CpyDplct" select="//BusMsg/AppHdr/CpyDplct"/>
<xsl:variable name="PssblDplct" select="//BusMsg/AppHdr/PssblDplct"/>
<xsl:variable name="Sgntr" select="//BusMsg/AppHdr/Sgntr"/>
<xsl:variable name="flagCpyDplcts">
<xsl:choose>
<xsl:when test="$CpyDplct = ''"/>
<xsl:when test="string-length($CpyDplct) <= 35 and ($CpyDplct = 'CODU' or $CpyDplct = 'COPY' or $CpyDplct = 'DUPL')">
<xsl:value-of select="$CpyDplct"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="$CpyDplct"/>
</xsl:copy>
<xsl:value-of select="'U0002'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagPssblDplcts">
<xsl:choose>
<xsl:when test="$PssblDplct = ''"/>
<xsl:when test="string-length($PssblDplct) <= 35 and ($PssblDplct = 'true' or $PssblDplct = 'false')">
<xsl:value-of select="$PssblDplct"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="$PssblDplct"/>
</xsl:copy>
<xsl:value-of select="'U0002'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagSgntrs">
<xsl:choose>
<xsl:when test="$Sgntr = ''"/>
<xsl:when test="string-length($Sgntr) <= 35">
<xsl:value-of select="$Sgntr"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="$PssblDplct"/>
</xsl:copy>
<xsl:value-of select="'U0012'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="/">
<ns:BusMsg xmlns:ns="urn:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso ../../../xsd/phase1/MainCIHub.xsd">
<ns1:AppHdr>
<xsl:call-template name="headers"/>
</ns1:AppHdr>
<ns:Document>
<ns:FIToFIPmtStsRpt>
<xsl:call-template name="bodys">
<xsl:with-param name="CtgyPurpB" select="//BusMsg/Document/FItoFICstmrCdtTrf/CdtTrfTxInf/PmtTpInf/CtgyPurp/Prtry"/>
<xsl:with-param name="IntrBkSttlmAmtB" select="//BusMsg/Document/FItoFICstmrCdtTrf/CdtTrfTxInf/IntrBkSttlmAmt/Value"/>
<xsl:with-param name="CcyB" select="//BusMsg/Document/FItoFICstmrCdtTrf/CdtTrfTxInf/IntrBkSttlmAmt/Ccy"/>
</xsl:call-template>
</ns:FIToFIPmtStsRpt>
</ns:Document>
</ns:BusMsg>
</xsl:template>
<xsl:template name="headers">
<ns1:CpyDplct>
<xsl:value-of select="$flagCpyDplcts"/>
</ns1:CpyDplct>
<ns1:PssblDplct>
<xsl:value-of select="$flagPssblDplcts"/>
</ns1:PssblDplct>
<ns1:Sgntr>
<xsl:value-of select="$flagSgntrs"/>
</ns1:Sgntr>
</xsl:template>
<xsl:template name="bodys">
<xsl:param name="CtgyPurpB"/>
<xsl:param name="IntrBkSttlmAmtB"/>
<xsl:param name="CcyB"/>
<xsl:param name="header"/>
<xsl:variable name="flagCtgyPurpBs">
<xsl:variable name="payT" select="substring($CtgyPurpB,1,3)"/>
<xsl:variable name="catP" select="substring($CtgyPurpB,4,2)"/>
<xsl:choose>
<xsl:when test="not(contains($payT,'010') or contains($payT,'011') or contains($payT,'019') or contains($payT,'110') or contains($payT,'510') or contains($payT,'610') or contains($payT,'710') or contains($payT,'720') or contains($payT,'000'))">
<xsl:value-of select="'U0002'"/>
</xsl:when>
<xsl:when test="not(contains($catP,'01') or contains($catP,'02') or contains($catP,'03') or contains($catP,'99'))">
<xsl:value-of select="'U0002'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$CtgyPurpB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagIntrBkSttlmAmtBs">
<xsl:variable name="amount" select="substring-before($IntrBkSttlmAmtB,'.')"/>
<xsl:variable name="decimal" select="substring-after($IntrBkSttlmAmtB,'.')"/>
<xsl:choose>
<xsl:when test="not(fn:matches($IntrBkSttlmAmtB, '^[0-9.]*$'))">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:when test="not(string-length($amount) <= 16)">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:when test="not(string-length($decimal) <= 2)">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$IntrBkSttlmAmtB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flagCcyBs">
<xsl:choose>
<xsl:when test="not(fn:matches($CcyB, '^[a-zA-Z0-9]*$') and string-length($CcyB) <= 3)">
<xsl:value-of select="'U0038'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$CcyB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ns1:CtgyPurp>
<xsl:value-of select="$flagCtgyPurpBs"/>
</ns1:CtgyPurp>
<ns1:IntrBkSttlmAmt>
<xsl:value-of select="$flagIntrBkSttlmAmtBs"/>
</ns1:IntrBkSttlmAmt>
<ns1:Ccy>
<xsl:value-of select="$flagCcyBs"/>
</ns1:Ccy>
<xsl:variable name="TxRsnStsB">
<xsl:choose>
<xsl:when test="contains($flagSgntrs, 'U0012') or contains($flagSgntrs, 'U0002') or contains($flagSgntrs, 'U0038') or contains($flagPssblDplcts, 'U0012') or contains($flagPssblDplcts, 'U0002') or contains($flagPssblDplcts, 'U0038') or contains($flagCpyDplcts, 'U0012') or contains($flagCpyDplcts, 'U0002') or contains($flagCpyDplcts, 'U0038') or contains($flagCtgyPurpBs, 'U0012') or contains($flagCtgyPurpBs, 'U0002') or contains($flagCtgyPurpBs, 'U0038') or contains($flagIntrBkSttlmAmtBs, 'U0012') or contains($flagIntrBkSttlmAmtBs, 'U0002') or contains($flagIntrBkSttlmAmtBs, 'U0038') or contains($flagCcyBs, 'U0012') or contains($flagCcyBs, 'U0002') or contains($flagCcyBs, 'U0038')">
<xsl:value-of select="'Decline'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'Approve'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ns1:TxRsnSts><xsl:value-of select="$TxRsnStsB"/></ns1:TxRsnSts>
</xsl:template>
</xsl:stylesheet>

Otherwise XSLT1.0 - correct output

I´ve a problem at my xslt.
Here it is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 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:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GRP">
<xsl:copy>
<!--copy the data from ADD - ST to the GRP so it can be used in the mapping to set the delivery address from end customer-->
<xsl:for-each select ="./ADD">
<xsl:if test="./QUALIFIER='ST'">
<xsl:copy-of select="PARTY_NAME_1"/>
<xsl:copy-of select="STREET_1"/>
<xsl:copy-of select="CITY"/>
<xsl:copy-of select="POSTAL_CODE"/>
<xsl:copy-of select="COUNTRY_CODE"/>
</xsl:if>
<xsl:choose>
<xsl:when test="./QUALIFIER='CN'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER"/>
</CONTACT_NUMBER>
</xsl:when>
<xsl:otherwise>
<xsl:if test="./QUALIFIER='ST'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER"/>
</CONTACT_NUMBER>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!--copy all other nodes-->
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Can you tell me, whats my mistake at my xml?
At my code i get Contact_Number 2 times.
I just like one of them... If Contact at CN - use this, otherwise use from ST.
-> If contact at CN and ST use CN.
Edit: xml:
<SEEDELFOR>
<GRP>
<ADD>
<QUALIFIER>CN</QUALIFIER>
<IDENTIFIER></IDENTIFIER>
<AGENCY_CODE></AGENCY_CODE>
<PARTY_NAME_1></PARTY_NAME_1>
<STREET_1></STREET_1>
<CITY></CITY>
<POSTAL_CODE></POSTAL_CODE>
<COUNTRY_CODE></COUNTRY_CODE>
<CONTACT>
<QUALIFIER></QUALIFIER>
<NUMBER>1111111</NUMBER>
</CONTACT>
</ADD>
<ADD>
<QUALIFIER>ST</QUALIFIER>
<IDENTIFIER></IDENTIFIER>
<AGENCY_CODE></AGENCY_CODE>
<PARTY_NAME_1></PARTY_NAME_1>
<STREET_1></STREET_1>
<CITY></CITY>
<POSTAL_CODE></POSTAL_CODE>
<CONTACT>
<QUALIFIER></QUALIFIER>
<NUMBER>2222222</NUMBER>
</CONTACT>
</ADD>
</GRP>
</SEEDELFOR>
How can i correct my xslt?
I hope you can help me.
Best regards
Julian
One of the way you can achieve this is, rewriting the template GRP as following:
<xsl:template match="GRP">
<xsl:copy>
<!--copy the data from ADD - ST to the GRP so it can be used in the mapping
to set the delivery address from end customer -->
<xsl:variable name="contact_ST">
<xsl:for-each select="./ADD">
<xsl:if test="./QUALIFIER='ST'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER" />
</CONTACT_NUMBER>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="contact_CN">
<xsl:for-each select="./ADD">
<xsl:if test="./QUALIFIER='CN'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER" />
</CONTACT_NUMBER>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="./ADD">
<xsl:if test="./QUALIFIER='ST'">
<xsl:copy-of select="PARTY_NAME_1" />
<xsl:copy-of select="STREET_1" />
<xsl:copy-of select="CITY" />
<xsl:copy-of select="POSTAL_CODE" />
<xsl:copy-of select="COUNTRY_CODE" />
</xsl:if>
</xsl:for-each>
<xsl:choose>
<xsl:when test="$contact_CN != ''">
<xsl:copy-of select="$contact_CN" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$contact_ST" />
</xsl:otherwise>
</xsl:choose>
<!--copy all other nodes -->
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
Consider the following simplified example:
<xsl:template match="GRP">
<xsl:copy>
<!-- other code, if necessary-->
<xsl:variable name="cn" select="ADD[QUALIFIER='CN']" />
<xsl:variable name="st" select="ADD[QUALIFIER='ST']" />
<CONTACT_NUMBER>
<xsl:choose>
<xsl:when test="$cn">
<xsl:value-of select="$cn/CONTACT/NUMBER"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$st/CONTACT/NUMBER"/>
</xsl:otherwise>
</xsl:choose>
</CONTACT_NUMBER>
<!-- more code, if necessary-->
</xsl:copy>
</xsl:template>

How to replace single apostrophe to double apostrophe in XSLT 1.0?

I need to replace a single quote to double quotes.
For example: input = Ram's
So i need a output as Ram''s
How to do in xslt 1.0? Please help me!
Its working for me!
<xsl:template match="name">
<xsl:copy>
<xsl:call-template name="replace">
<!-- Here getting the replaced values -->
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="text"/>
<xsl:param name="searchString">'</xsl:param>
<xsl:param name="replaceString">''</xsl:param>
<xsl:choose>
<xsl:when test="contains($text,$searchString)">
<xsl:value-of select="substring-before($text,$searchString)"/>
<xsl:value-of select="$replaceString"/>
<!-- recursive call -->
<xsl:call-template name="replace">
<xsl:with-param name="text" select="substring-after($text,$searchString)"/>
<xsl:with-param name="searchString" select="$searchString"/>
<xsl:with-param name="replaceString" select="$replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

XSLT1.0 replace multiple occurences of char with <br />

Have the following template that I use with xsl:call-template, but I need to use it to replace a ~ with <br />. I can get it to work with none HTML replacements but not when I try to use <br /> or &NewLine; or
. Any suggestions:
<xsl:template name="replace-substring">
<xsl:param name="original"/>
<xsl:param name="substring"/>
<xsl:param name="replacement" select="''"/>
<xsl:variable name="first">
<xsl:choose>
<xsl:when test="contains($original, $substring)" >
<xsl:value-of select="substring-before($original, $substring)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$original"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="middle">
<xsl:choose>
<xsl:when test="contains($original, $substring)">
<xsl:value-of select="$replacement" />
</xsl:when>
<xsl:otherwise>
<xsl:text></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="last">
<xsl:choose>
<xsl:when test="contains($original, $substring)">
<xsl:choose>
<xsl:when test="contains(substring-after($original, $substring),
$substring)">
<xsl:call-template name="replace-substring">
<xsl:with-param name="original">
<xsl:value-of select="substring-after($original, $substring)"/>
</xsl:with-param>
<xsl:with-param name="substring">
<xsl:value-of select="$substring"/>
</xsl:with-param>
<xsl:with-param name="replacement">
<xsl:value-of select="$replacement"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after($original, $substring)" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($first, $middle, $last)"/>
</xsl:template>
It's hard to tell what's going on with the first, middle, and last variables but you should be able to just use a literal <br/> in your param...
XML
<test>one~two~three</test>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="test">
<xsl:copy>
<xsl:call-template name="replace-char"/>
</xsl:copy>
</xsl:template>
<xsl:template name="replace-char">
<xsl:param name="char" select="'~'"/>
<xsl:param name="replacement"><br/></xsl:param>
<xsl:param name="string" select="."/>
<xsl:variable name="remaining" select="substring-after($string,$char)"/>
<xsl:value-of select="substring-before(concat($string,$char),$char)"/>
<xsl:if test="contains($string,$char)">
<xsl:copy-of select="$replacement"/>
</xsl:if>
<xsl:if test="$remaining">
<xsl:call-template name="replace-char">
<xsl:with-param name="string" select="$remaining"/>
<xsl:with-param name="char" select="$char"/>
<xsl:with-param name="replacement" select="$replacement"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output
<test>one<br/>two<br/>three</test>

How can I eliminate a duplicate result in XSLT 1.0?

I am using the Altova mapping tool and I cannot find an option on how to eliminate a duplicate value so I am trying to update the XSLT file directly and I cannot figure out how to do this. Below is the XSLT file, the problem is in the Detail06 section.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <xsl:template match="/">
<AsyncBarcode>
<xsl:variable name="var1_instance" select="." />
<xsl:for-each select="$var1_instance/n:SyncReceiveDelivery">
<xsl:variable name="var2_SyncReceiveDelivery" select="." />
<Prefix>
<xsl:for-each select="n:DataArea/n:Sync/n:AccountingEntityID">
<CompanyID>
<xsl:value-of select="string(.)" />
</CompanyID>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:Sync/n:AccountingEntityID">
<ExternalPartnerID>
<xsl:value-of select="string(.)" />
</ExternalPartnerID>
</xsl:for-each>
<DocumentType>
<xsl:value-of select="'AsyncBarcode'" />
</DocumentType>
<xsl:for-each select="n:ApplicationArea/n:BODID">
<DocumentNumber>
<xsl:value-of select="substring-after(substring-before(string(.), ':1?'), 'Infor:')" />
</DocumentNumber>
</xsl:for-each>
<TransactionDirection>
<xsl:value-of select="'I'" />
</TransactionDirection>
<DateStamp>
<xsl:value-of select="substring-before(string(n:ApplicationArea/n:CreationDateTime), 'T')" />
</DateStamp>
<TimeStamp>
<xsl:value-of select="substring-before(substring-after(string(n:ApplicationArea/n:CreationDateTime), 'T'), 'Z')" />
</TimeStamp>
<xsl:for-each select="n:ApplicationArea/n:BODID">
<ControlNumber>
<xsl:value-of select="substring-after(substring-before(string(.), ':1?'), 'Infor:')" />
</ControlNumber>
</xsl:for-each>
</Prefix>
<Header00>
<TransactionDefinitionKey>
<xsl:value-of select="'25'" />
</TransactionDefinitionKey>
<xsl:for-each select="n:DataArea/n:Sync/n:AccountingEntityID">
<CompanyID0>
<xsl:value-of select="string(.)" />
</CompanyID0>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryHeader/n:DocumentReference/n:DocumentID">
<xsl:variable name="var14_DocumentID" select="." />
<xsl:if test="$var14_DocumentID/n:ID/#location">
<BranchID>
<xsl:value-of select="substring-before(string(n:ID/#location), '-B')" />
</BranchID>
</xsl:if>
</xsl:for-each>
<UserID>
<xsl:value-of select="'WMUser'" />
</UserID>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryHeader/n:WarehouseLocation/n:ID">
<WarehouseID>
<xsl:value-of select="substring-before(string(.), '-W')" />
</WarehouseID>
</xsl:for-each>
</Header00>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryItem">
<Detail01>
<xsl:for-each select="$var2_SyncReceiveDelivery/n:DataArea/n:Sync/n:AccountingEntityID">
<Scanneddata1>
<xsl:value-of select="string(.)" />
</Scanneddata1>
</xsl:for-each>
</Detail01>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery">
<xsl:variable name="var22_ReceiveDelivery" select="." />
<xsl:for-each select="n:ReceiveDeliveryItem">
<Detail02>
<xsl:for-each select="$var22_ReceiveDelivery/n:ReceiveDeliveryHeader/n:DocumentReference/n:DocumentID">
<xsl:variable name="var26_DocumentID" select="." />
<xsl:if test="$var26_DocumentID/n:ID/#location">
<Scanneddata2>
<xsl:value-of select="substring-before(string(n:ID/#location), '-B')" />
</Scanneddata2>
</xsl:if>
</xsl:for-each>
</Detail02>
</xsl:for-each>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery">
<xsl:variable name="var28_ReceiveDelivery" select="." />
<xsl:for-each select="n:ReceiveDeliveryItem">
<Detail03>
<xsl:for-each select="$var28_ReceiveDelivery/n:ReceiveDeliveryHeader/n:DocumentReference/n:DocumentID">
<Scanneddata3>
<xsl:value-of select="string(n:ID)" />
</Scanneddata3>
</xsl:for-each>
</Detail03>
</xsl:for-each>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryItem">
<Detail04>
<xsl:for-each select="n:LineNumber">
<Scanneddata4>
<xsl:value-of select="string(.)" />
</Scanneddata4>
</xsl:for-each>
</Detail04>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryItem">
<xsl:variable name="var38_ReceiveDeliveryItem" select="." />
<Detail05>
<xsl:variable name="var40_map_select_SerializedLot">
<xsl:if test="string((n:SerializedLot/n:Lot/n:LotIDs/n:ID) = (n:SerializedLot/n:Lot/n:LotIDs/n:ID)) != 'false'">
<xsl:value-of select="'1'" />
</xsl:if>
</xsl:variable>
<xsl:variable name="var48_">
<xsl:choose>
<xsl:when test="string(boolean(string($var40_map_select_SerializedLot))) != 'false'">
<xsl:variable name="var45_map_select_SerializedLot">
<xsl:for-each select="n:SerializedLot/n:Lot/n:LotIDs/n:ID">
<xsl:value-of select="string(.)" />
</xsl:for-each>
</xsl:variable>
<xsl:variable name="var41_map_select_SerializedLot">
<xsl:if test="string((string($var45_map_select_SerializedLot)) = (string($var45_map_select_SerializedLot))) != 'false'">
<xsl:value-of select="'1'" />
</xsl:if>
</xsl:variable>
<xsl:if test="string(boolean(string($var41_map_select_SerializedLot))) != 'false'">
<xsl:variable name="var42_map_select_SerializedLot">
<xsl:for-each select="n:SerializedLot/n:Lot/n:LotIDs/n:ID">
<xsl:value-of select="string(.)" />
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="string($var42_map_select_SerializedLot)" />
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="' '" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<Scanneddata5>
<xsl:copy-of select="$var48_" />
</Scanneddata5>
</Detail05>
</xsl:for-each>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryItem">
<Detail06>
<xsl:for-each select="n:HoldCodes/n:Code">
<Scanneddata6>
<xsl:choose>
<xsl:when test="string((' ' != string(.))) != 'false'">
<xsl:value-of select="'Hold'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'Inventory'" />
</xsl:otherwise>
</xsl:choose>
</Scanneddata6>
</xsl:for-each>
</Detail06>
</xsl:for-each>
<Detail07>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryHeader/n:ReceivedDateTime">
<Scanneddata7>
<xsl:value-of select="string(.)" />
</Scanneddata7>
</xsl:for-each>
</Detail07>
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryItem">
<Detail11>
<xsl:for-each select="n:ReceivedQuantity">
<Scanneddata11>
<xsl:value-of select="number(string(.))" />
</Scanneddata11>
</xsl:for-each>
</Detail11>
</xsl:for-each>
</xsl:for-each>
</AsyncBarcode>
</xsl:template>
</xsl:stylesheet>
Below is the input I am receiving:
<SyncReceiveDelivery xmlns="http://schema.infor.com/InforOAGIS/2" releaseID="9.2" versionID="2.8.0">
<ApplicationArea>
<Sender>
<LogicalID>lid://</LogicalID>
<ComponentID>Warehouse Management</ComponentID>
<ReferenceID accountingEntity="01" location="RS01-W">0000058141</ReferenceID>
</Sender>
<CreationDateTime>2015-03-31T20:08:16Z</CreationDateTime>
<BODID>infor-nid:infor:01:RS01-W:0000002445:62883?ReceiveDelivery&verb=Sync</BODID>
</ApplicationArea>
<DataArea>
<Sync>
<TenantID>infor</TenantID>
<AccountingEntityID>01</AccountingEntityID>
<LocationID accountingEntity="01">RS01-W</LocationID>
<ActionCriteria>
<ActionExpression actionCode="Add" />
</ActionCriteria>
</Sync>
<ReceiveDelivery>
<ReceiveDeliveryHeader>
<DocumentID>
<ID accountingEntity="01" location="RS01-W" variationID="62883">0000002445</ID>
</DocumentID>
<LastModificationDateTime>2015-03-31T20:08:12Z</LastModificationDateTime>
<DocumentDateTime>2015-03-31T20:08:12Z</DocumentDateTime>
<DocumentReference type="CustomerReturn">
<DocumentID>
<ID accountingEntity="01" location="1323-B">930131</ID>
</DocumentID>
</DocumentReference>
<Status>
<Code listID="ReceiveDeliveryStatus">Received</Code>
</Status>
<WarehouseLocation>
<ID accountingEntity="01">RS01-W</ID>
<Name languageID="en-US">Power Packaging</Name>
<Address>
<AddressLine sequence="1">401 N. Main</AddressLine>
<CityName>Rosendale</CityName>
<CountrySubDivisionCode>WI</CountrySubDivisionCode>
<PostalCode listID="PostalCode">54974</PostalCode>
</Address>
</WarehouseLocation>
<ActualDeliveryDateTime>2015-03-31T17:31:46Z</ActualDeliveryDateTime>
<GrossWeightMeasure unitCode="LB">8120.4147</GrossWeightMeasure>
<TotalVolumeMeasure unitCode="CF">0</TotalVolumeMeasure>
<ShipFromParty>
<Location>
<ID>30155</ID>
<Name languageID="en-US">RS-IFP-USFS HOUSTON</Name>
<Address>
<AddressLine sequence="1">USFS HOUSTON</AddressLine>
<AddressLine sequence="2">111 ALIANT DRIVE</AddressLine>
<CityName>HOUSTON</CityName>
<CountrySubDivisionCode>TX</CountrySubDivisionCode>
<CountryCode>USA</CountryCode>
<PostalCode listID="PostalCode">77032</PostalCode>
</Address>
</Location>
</ShipFromParty>
<ReceivedDateTime>2015-03-31T20:08:12Z</ReceivedDateTime>
<DeliverToParty>
<Location>
<ID accountingEntity="01">RS01-W</ID>
<Name languageID="en-US">Power Packaging</Name>
<Address>
<AddressLine sequence="1">401 N. Main</AddressLine>
<CityName>Rosendale</CityName>
<CountrySubDivisionCode>WI</CountrySubDivisionCode>
<PostalCode listID="PostalCode">54974</PostalCode>
</Address>
</Location>
</DeliverToParty>
<ASNReference>
<DocumentID>
<ID accountingEntity="01" location="RS01-W">0000002445</ID>
</DocumentID>
</ASNReference>
</ReceiveDeliveryHeader>
<ReceiveDeliveryItem>
<ItemID>
<ID accountingEntity="01">200135-100250</ID>
</ItemID>
<ServiceIndicator>false</ServiceIndicator>
<Description languageID="en-US">Orchard Splash 12/25 fl oz Orange Gold 100</Description>
<Note languageID="en-US">1</Note>
<DocumentReference type="CustomerReturn">
<DocumentID>
<ID accountingEntity="01" location="1323-B">930131</ID>
</DocumentID>
<LineNumber>1</LineNumber>
</DocumentReference>
<PackingSlipQuantity unitCode="CS">0.0</PackingSlipQuantity>
<PackingSlipBaseUOMQuantity unitCode="CS">0.0</PackingSlipBaseUOMQuantity>
<ReceivedQuantity unitCode="CS">90.0</ReceivedQuantity>
<ReceivedBaseUOMQuantity unitCode="CS">90.0</ReceivedBaseUOMQuantity>
<ReturnedQuantity unitCode="CS">0.0</ReturnedQuantity>
<ReturnedBaseUOMQuantity unitCode="CS">0.0</ReturnedBaseUOMQuantity>
<SerializedLot>
<ItemQuantity unitCode="CS">90.0</ItemQuantity>
<ItemBaseUOMQuantity unitCode="CS">90.0</ItemBaseUOMQuantity>
<Lot>
<LotIDs>
<ID>RS1412107</ID>
</LotIDs>
<Quantity unitCode="CS">90.0</Quantity>
<BaseUOMQuantity unitCode="CS">90.0</BaseUOMQuantity>
</Lot>
</SerializedLot>
<LineNumber>1</LineNumber>
<HoldCodes>
<Code listID="Hold Reason Codes">HOLD</Code>
</HoldCodes>
<HoldCodes>
<Code listID="Hold Reason Codes">QCREQ</Code>
</HoldCodes>
<CountSequence>1</CountSequence>
</ReceiveDeliveryItem>
<ReceiveDeliveryItem>
<ItemID>
<ID accountingEntity="01">200135-100252</ID>
</ItemID>
<ServiceIndicator>false</ServiceIndicator>
<Description languageID="en-US">Orchard Hills 12/25 fl oz Orange 100</Description>
<Note languageID="en-US">2</Note>
<DocumentReference type="CustomerReturn">
<DocumentID>
<ID accountingEntity="01" location="1323-B">930131</ID>
</DocumentID>
<LineNumber>2</LineNumber>
</DocumentReference>
<PackingSlipQuantity unitCode="CS">0.0</PackingSlipQuantity>
<PackingSlipBaseUOMQuantity unitCode="CS">0.0</PackingSlipBaseUOMQuantity>
<ReceivedQuantity unitCode="CS">90.0</ReceivedQuantity>
<ReceivedBaseUOMQuantity unitCode="CS">90.0</ReceivedBaseUOMQuantity>
<ReturnedQuantity unitCode="CS">0.0</ReturnedQuantity>
<ReturnedBaseUOMQuantity unitCode="CS">0.0</ReturnedBaseUOMQuantity>
<SerializedLot>
<ItemQuantity unitCode="CS">90.0</ItemQuantity>
<ItemBaseUOMQuantity unitCode="CS">90.0</ItemBaseUOMQuantity>
<Lot>
<LotIDs>
<ID>RS141112</ID>
</LotIDs>
<Quantity unitCode="CS">90.0</Quantity>
<BaseUOMQuantity unitCode="CS">90.0</BaseUOMQuantity>
</Lot>
</SerializedLot>
<LineNumber>2</LineNumber>
<HoldCodes>
<Code listID="Hold Reason Codes">HOLD</Code>
</HoldCodes>
<HoldCodes>
<Code listID="Hold Reason Codes">QCREQ</Code>
</HoldCodes>
<CountSequence>1</CountSequence>
</ReceiveDeliveryItem>
<ReceiveDeliveryItem>
<ItemID>
<ID accountingEntity="01">200135-100252</ID>
</ItemID>
<ServiceIndicator>false</ServiceIndicator>
<Description languageID="en-US">Orchard Hills 12/25 fl oz Orange 100</Description>
<Note languageID="en-US">3</Note>
<DocumentReference type="CustomerReturn">
<DocumentID>
<ID accountingEntity="01" location="1323-B">930131</ID>
</DocumentID>
<LineNumber>3</LineNumber>
</DocumentReference>
<PackingSlipQuantity unitCode="CS">0.0</PackingSlipQuantity>
<PackingSlipBaseUOMQuantity unitCode="CS">0.0</PackingSlipBaseUOMQuantity>
<ReceivedQuantity unitCode="CS">90.0</ReceivedQuantity>
<ReceivedBaseUOMQuantity unitCode="CS">90.0</ReceivedBaseUOMQuantity>
<ReturnedQuantity unitCode="CS">0.0</ReturnedQuantity>
<ReturnedBaseUOMQuantity unitCode="CS">0.0</ReturnedBaseUOMQuantity>
<SerializedLot>
<ItemQuantity unitCode="CS">90.0</ItemQuantity>
<ItemBaseUOMQuantity unitCode="CS">90.0</ItemBaseUOMQuantity>
<Lot>
<LotIDs>
<ID>RS1412030</ID>
</LotIDs>
<Quantity unitCode="CS">90.0</Quantity>
<BaseUOMQuantity unitCode="CS">90.0</BaseUOMQuantity>
</Lot>
</SerializedLot>
<LineNumber>3</LineNumber>
<HoldCodes>
<Code listID="Hold Reason Codes">HOLD</Code>
</HoldCodes>
<HoldCodes>
<Code listID="Hold Reason Codes">QCREQ</Code>
</HoldCodes>
<CountSequence>1</CountSequence>
</ReceiveDeliveryItem>
</ReceiveDelivery>
</DataArea>
</SyncReceiveDelivery>
Finally, this is what I am expecting however I am getting duplicates values in Scannedata6 for the Detail06 section:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE AsyncBarcode SYSTEM "C:/InboundBODS/AsyncBarcode_Inbound.dtd">
<AsyncBarcode>
<Prefix>
<CompanyID>01</CompanyID>
<ExternalPartnerID>01</ExternalPartnerID>
<DocumentType>AsyncBarcode</DocumentType>
<DocumentNumber/>
<TransactionDirection>I</TransactionDirection>
<DateStamp>2015-03-31</DateStamp>
<TimeStamp>20:08:16</TimeStamp>
<ControlNumber/>
</Prefix>
<Header00>
<TransactionDefinitionKey>25</TransactionDefinitionKey>
<CompanyID0>01</CompanyID0>
<BranchID>1323</BranchID>
<UserID>WMUser</UserID>
<WarehouseID>RS01</WarehouseID>
</Header00>
<Detail01>
<Scanneddata1>01</Scanneddata1>
</Detail01>
<Detail01>
<Scanneddata1>01</Scanneddata1>
</Detail01>
<Detail01>
<Scanneddata1>01</Scanneddata1>
</Detail01>
<Detail02>
<Scanneddata2>1323</Scanneddata2>
</Detail02>
<Detail02>
<Scanneddata2>1323</Scanneddata2>
</Detail02>
<Detail02>
<Scanneddata2>1323</Scanneddata2>
</Detail02>
<Detail03>
<Scanneddata3>930131</Scanneddata3>
</Detail03>
<Detail03>
<Scanneddata3>930131</Scanneddata3>
</Detail03>
<Detail03>
<Scanneddata3>930131</Scanneddata3>
</Detail03>
<Detail04>
<Scanneddata4>1</Scanneddata4>
</Detail04>
<Detail04>
<Scanneddata4>2</Scanneddata4>
</Detail04>
<Detail04>
<Scanneddata4>3</Scanneddata4>
</Detail04>
<Detail05>
<Scanneddata5>RS1412107</Scanneddata5>
</Detail05>
<Detail05>
<Scanneddata5>RS141112</Scanneddata5>
</Detail05>
<Detail05>
<Scanneddata5>RS1412030</Scanneddata5>
</Detail05>
<Detail06>
<Scanneddata6>Hold</Scanneddata6>
</Detail06>
<Detail06>
<Scanneddata6>Hold</Scanneddata6>
</Detail06>
<Detail06>
<Scanneddata6>Hold</Scanneddata6>
</Detail06>
<Detail07>
<Scanneddata7>2015-03-31T20:08:12Z</Scanneddata7>
</Detail07>
<Detail11>
<Scanneddata11>90</Scanneddata11>
</Detail11>
<Detail11>
<Scanneddata11>90</Scanneddata11>
</Detail11>
<Detail11>
<Scanneddata11>90</Scanneddata11>
</Detail11>
</AsyncBarcode>
Was able to figure out what I needed to change. Change shown below:
<xsl:for-each select="n:DataArea/n:ReceiveDelivery/n:ReceiveDeliveryItem/n:HoldCodes[1]">
<Detail06>
<xsl:for-each select="n:Code[1]">
<Scanneddata6>
<xsl:choose>
<xsl:when test="string(('' != string(.))) != 'false'">
<xsl:value-of select="'Hold'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'Inventory'" />
</xsl:otherwise>
</xsl:choose>
</Scanneddata6>
</xsl:for-each>
</Detail06>
</xsl:for-each>