Converting integer values to string in XSLT 1.0 - xslt

I have an element in my XSLT named profileid. The value to this element can be expected as a set of numbers that is right now going as integer(eg. 452628), but this value has to be passed as a string to the next process. Is there a way I can convert this integer value to string within xslt itself? [I am using XSLT 1.0]
XSLT I've used:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" method="xml"/>
<xsl:template match="/">
<Session>
<xsl:variable name="profileID" select="concat(' ',$profileid)"/>
<profileId>
<xsl:value-of select="translate($profileID,' ','')"/>
</profileId>
</Session>
</xsl:template>
</xsl:stylesheet>
(Converted the xml response to json)
JSON output :-
{ "Session": { "profileId": 452628 } }
Expected JSON output:_
{ "Session": { "profileId": "452628" } }
A way to convert this integer value to String inside xslt

Assuming you want all the attributes as String values. After the XSLT transformation set the following property using JSON Transform Mediator
<jsontransform>
<property name="synapse.commons.json.output.autoPrimitive" value = "false"/>
</jsontransform>

Related

XSL query inner xml

Say this is my xml :
<History>
<University>TSU</University>
<Payload>
<Attrib Order="0">OVERSEA</Attrib>
<Attrib Order="1">GRADE2</Attrib>
<Attrib Order="2"><Person><ID>TQR344</ID></Person></Attrib>
<Attrib Order="3">3566644</Attrib>
</Payload>
</History>
And I want to query the inner XML inside Order=2 tag and read ID of the person.
I have created this so far :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />
<xsl:template match="/History">
<xsl:apply-templates select="/History" />
</xsl:template>
<xsl:template name="Person" match="//History">
<Event>
<Uni><xsl:value-of select="University" /></Uni>
<ID><xsl:value-of select="Payload/Attrib[#Order='2']/Person/ID" disable-output-escaping="yes" /></ID>
</Event>
</xsl:template>
</xsl:stylesheet>
But as you can see it is not working.
Also I assigned the inner XML into a variable and tried to query that variable and It didn't work too.
Is it possible to do that via xsl ?
Limitations : I cannot change xml format. But maybe I was able to move from xsl ver 1 to new versions.
I want to query the inner XML inside Order=2 tag
The tag in question does not contain any XML; its content is a string and needs to be manipulated using string functions. Try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/History">
<Event>
<Uni>
<xsl:value-of select="University" />
</Uni>
<ID>
<xsl:value-of select="substring-before(substring-after(Payload/Attrib[#Order='2'], '<ID>'),'</ID><')"/>
</ID>
</Event>
</xsl:template>
</xsl:stylesheet>
Note:
1. This:
<xsl:template match="/History">
<xsl:apply-templates select="/History" />
</xsl:template>
creates an infinite loop and will crash your processor.
2. Alternatively, you could serialize the string back into XML and process the result as XML; in XSLT 1.0, this can be done only by outputting the string with the escaping disabled, saving the result as a new document, then processing the new document with another XSLT stylesheet. Using XSLT 3.0 (or a processor that supports serializing as an extension) this can be all done during the same transformation.

Passing and parsing XML as a parameter to XSLT 2.0

I'm trying to figure out how to pass XML into a XSLT document and parse it as you would on any node.
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"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="xmlparam"></xsl:param>
<xsl:template match="/">
<xsl:value-of select="node()"></xsl:value-of>
<xsl:value-of select="$xmlparam/coll"></xsl:value-of>
</xsl:template>
</xsl:stylesheet>
xmlparam:
<?xml version="1.0" encoding="UTF-8"?>
<coll>
<title>Test</title>
<text>Some text</text>
</coll>
Input:
<?xml version="1.0" encoding="UTF-8"?>
<coll>
Root doc
</coll>
Output:
<?xml version="1.0" encoding="UTF-8"?>
Root doc
XPTY0019: Required item type of first operand of '/' is node(); supplied value has item type xs:untypedAtomic
Does anyone know how to parse XML passed in as a parameter to XSLT? Due to certain restraints, I cannot read in a file, it needs to be a parameter.
You could get your XML input to be parsed by the style sheet by using the document function, e.g. like (from memory, so maybe not completely accurate)
<xsl:variable name="myData" select="document('myData')"/>
AND registering a custom URIResolver with the with your Transformer. This custom URIResolver will get "myData" as value of the parameter href of its resolve method and could then obtain the content of the document from e.g. a String. This would give you roughly the same flexibility as adding the content as a parameter.
Code sketch (assuming the obvious implementation of MyURIResolver):
Transformer myTransformer = getMyTransformer();
String myXmlDocument = getMyXmlDocumetAsString();
URIResolver myURIResolver = new MyURIResolver();
myURIResolver.put("myData", myXmlDocument);
myTransformer.setURIResolver(myURIResolver);
myTransformer.transform(source, result);

XSL Attribute expression notation in text nodes

value-of is horrible. When I need to insert a large number of variable values into a text node, it really pollutes the XSL file.
Is there a way to be able to use attribute expression notation, i.e. text text {$variable}, on the inside of an output text node? Or at least something more concise than value-of?
Not in XSLT 1.0. However, in XSLT 3.0 you can use TVTs (text value templates). They work the same as AVTs (attribute value templates).
To use a TVT, add the standard attribute xsl:expand-text="yes" to the element. This will cause the processor to treat descendant text nodes of that element as a TVT.
Example:
XSLT 3.0
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="who" select="'Dan'"/>
<xsl:variable name="what" select="'BAM!'"/>
<result xsl:expand-text="yes">This is {$who}'s result: {$what}</result>
</xsl:template>
</xsl:stylesheet>
Output (using any well-formed XML as input)
<result>This is Dan's result: BAM!</result>
Note: Tested using Saxon-PE 9.5.
Here's a better example showing the "descendant" text nodes being evaluated...
XML Input
<test>
<v1>one</v1>
<v2>two</v2>
<v3>three</v3>
</test>
XSLT 3.0
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/*">
<result xsl:expand-text="yes">
<value>Value of v1: {v1}</value>
<value>Value of v2: {v2}</value>
<value>Value of v3: {v3}</value>
</result>
</xsl:template>
</xsl:stylesheet>
Output
<result>
<value>Value of v1: one</value>
<value>Value of v2: two</value>
<value>Value of v3: three</value>
</result>
You can use concat.
<xsl:value-of select="concat(
'text text ',
$variable,
'text text ',
$variable,
'text text'
)" />

xslt parse XML string into variable and use Xpath

My (simplified) input XML file contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<DATA_RECORD>
<MESSAGE><pd>
<cdhead version="13"/>
</pd></MESSAGE>
</DATA_RECORD>
</main>
The MESSAGE element value is a character-escaped XML instance. It represents the following XML:
<pd>
<cdhead version="13"/>
</pd>
I would like to apply an xsl transformation on the input XML and somehow parse the MESSAGE contents into a variable and use Xpath expressions to access its details.
I tried adding a javascript function as below, but the object returned by the script apparently is of an incorrect DOM subclass (see result underneath). For completeness, I added an extra function that returns the DOM contents as a string.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:my="http://example.com/my"
exclude-result-prefixes="ms my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<ms:script language="JScript" implements-prefix="my">
<![CDATA[
function parseToDOM (input) {
var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
doc.loadXML (input);
return doc.documentElement;
};
function parseToXMLString (input) {
var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
doc.loadXML (input);
return doc.documentElement.xml;
};
]]>
</ms:script>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="DATA_RECORD">
<xsl:variable name="DOM"><xsl:copy-of select="my:parseToDOM (MESSAGE)"/></xsl:variable>
<xsl:variable name="XML"><xsl:copy-of select="my:parseToXMLString (MESSAGE)"/></xsl:variable>
<msg1><xsl:value-of select="$XML"/></msg1>
<msg2><xsl:value-of select="$XML" disable-output-escaping="yes"/></msg2>
<dom><xsl:copy-of select="$DOM"/></dom>
<version><xsl:value-of select="$DOM/pd/cdhead/#version"/></version>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<msg1><pd>
<cdhead version="13"/>
</pd></msg1>
<msg2><pd>
<cdhead version="13"/>
</pd></msg2>
<dom/>
<version></version>
</root>
How can I make the Jscript function return a result that allows the use of Xpath?
By the way, is there some XSLT 1.0 function available that allows parsing the escaped XML string to a result that allows the use of Xpath?
ADDITION
I have been trying some variations and got closer to a solution. First, Altova XMLSpy allows choosing the xsl processor, and the above resulted when using the built-in one. Of course I need MSXML 6.0 and when choosing that one, errors occurred as I had to parse input.text instead. But I only succeeded in being able to use Xpath expressions in the result after doing extra stuff in the javascript. It transpired that while < and the like are parsed into < etcetera, this is not enough to arrive at the proper DOM result. So I resorted to unescaping the input string first.
But I hit another snag: where the below works fine, it does not when I use input.text instead of the literal below.
See below the xslt.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:my="http://example.com/my"
exclude-result-prefixes="ms my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<ms:script language="JScript" implements-prefix="my">
<![CDATA[
function parseToDOM (input) {
var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
doc.loadXML (unescapeXML ('<pd>
<cdhead version="13"/>
</pd>'));
//doc.loadXML (unescapeXML (input.text));
return doc;
};
function unescapeXML (str) {
var ostr = str;
ostr = ostr.replace (/"/g, '"');
ostr = ostr.replace (/</g, '<');
ostr = ostr.replace (/=/g, '=');
ostr = ostr.replace (/>/g, '>');
return ostr;
};
]]>
</ms:script>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="DATA_RECORD">
<xsl:variable name="msg" select="my:parseToDOM (MESSAGE)"/>
<tst><xsl:value-of select="$msg/pd/cdhead/#version"/></tst>
</xsl:template>
</xsl:stylesheet>
Now results in
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tst>13</tst>
</root>
Which is exactly what I want.
But as remarked above, when I comment the parsing of the literal and use the input instead, like so:
//doc.loadXML (unescapeXML ('<pd>
<cdhead version="13"/>
</pd>'));
doc.loadXML (unescapeXML (input.text));
I get the following error (in Altova XML Spy with MSXML 6.0 as xslt parser):
XSL transformation failed due to following error:
Microsoft JScript runtime error
'undefined' is null or not an object
line = 10, col = 3 (line is offset from the start of the script block).
Error returned from property or method call.
Which points at the first javascript replace statement.
And also, IE9 cannot process the following properly:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<main>
<DATA_RECORD>
<MESSAGE><pd>
<cdhead version="13"/>
</pd></MESSAGE>
</DATA_RECORD>
</main>
When I open this file in IE9 (where test.xslt is the version of the transformation where the input is ignored and instead a literal is processed, hence the one that is OK in XML Spy), I get a processing error:
XML5001: Applying Integrated XSLT Handling.
XSLT8690: XSLT processing failed.
Why is all this and how can I correct it?
Starting from the ADDITION above, I reached a solution by finetuning it a little.
To avoid having to do input.text and use plain input instead, the xsl has to contain a conversion of the element to a string by applying the xslt string function (I thought it was a string already, but apparently that is not the case). In addition, it was not necessary any more to apply the replace statements now.
Thus
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:my="http://example.com/my"
exclude-result-prefixes="ms my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<ms:script language="JScript" implements-prefix="my">
<![CDATA[
function parseToDOM (input) {
var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
doc.loadXML (input);
return doc;
};
]]>
</ms:script>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="DATA_RECORD">
<xsl:variable name="msg" select="my:parseToDOM (string(MESSAGE))"/>
<tst><xsl:value-of select="$msg/pd/cdhead/#version"/></tst>
</xsl:template>
</xsl:stylesheet>
works: when applied on
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<main>
<DATA_RECORD>
<MESSAGE><pd>
<cdhead version="13"/>
</pd></MESSAGE>
</DATA_RECORD>
</main>
the result is
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tst>13</tst>
</root>
Unluckily, IE9 still fails in loading the XML with referred XSLT; and I discovered why.
I had to tick the box in Internet Options/Advanced/Security/Allow active content to run in files on My Computer - and also restart IE - this makes IE9 process the file correctly. Of course, the result not being html means that the result can only be viewed in F12/Script tab, but this was just an example and I will incorporate it in an xslt that generates proper html.

BizTalk Map Name Value Pair to Hierarchical Schema

I'm going to be working the MS AX2010. When accessing data through an AX WCF service, the response is XML containing name / value pairs - known as a key data list. I'll be collecting this XML in BizTalk and needing to transform it to a canonical hierarchical schema. So for example, if I read a source Name element with "OrderNumber", then I would map the associated Value to an OrderNumber element in the destination schema.
Has anyone discovered a nice way to do this using a BizTalk map?
I acknowledge that you prefer to use the graphical functoids, but if you can accept an xslt route, it is pretty straightforward (See here for converting a visual map to an xslt). eg. the following XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="inputxmlns"
xmlns:ns1="outputxmlns"
exclude-result-prefixes="ns0"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/ns0:Root">
<ns1:Root>
<ns1:Elements>
<xsl:for-each select="ns0:Elements/ns0:Element">
<xsl:element name="ns1:{normalize-space(*[local-name()='Name']/text())}">
<xsl:value-of select="ns0:Value/text()"/>
</xsl:element>
</xsl:for-each>
</ns1:Elements>
</ns1:Root>
</xsl:template>
</xsl:stylesheet>
Will transform a quasi EAV schema:
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="inputxmlns">
<Elements>
<Element>
<Name>
NameOfElement1
</Name>
<Value>
ValueOfElement1
</Value>
</Element>
<Element>
<Name>
NameOfElement2
</Name>
<Value>
ValueOfElement2
</Value>
</Element>
</Elements>
</Root>
To this:
<?xml version="1.0" encoding="utf-8"?>
<ns1:Root xmlns:ns1="outputxmlns">
<ns1:Elements>
<ns1:NameOfElement1>
ValueOfElement1
</ns1:NameOfElement1>
<ns1:NameOfElement2>
ValueOfElement2
</ns1:NameOfElement2>
</ns1:Elements>
</ns1:Root>