Differentiate between similar records - xslt-1.0

I have some input XML that is auto generated(so I am unable to rename the fields accordingly):
<?xml version="1.0" encoding="UTF-8"?>
<csv-xml>
<record>
<csv-field-1>1</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>7654321</csv-field-3>
<csv-field-4>1</csv-field-4>
<csv-field-5>08/08/19</csv-field-5>
<csv-field-6>08/08/19</csv-field-6>
</record>
<record>
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>12345678</csv-field-3>
<csv-field-4>3</csv-field-4>
</record>
<record>
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>22345679</csv-field-3>
<csv-field-4>7</csv-field-4>
</record>
<record>
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>32345680</csv-field-3>
<csv-field-4>6</csv-field-4>
</record>
<record>
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>42345681</csv-field-3>
<csv-field-4>2</csv-field-4>
</record>
<record>
<csv-field-1>3</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3></csv-field-3>
</record>
</csv-xml>
I am trying to figure out how to use an XSLT transformation to take out the data I need when records have the same path/name.
I have tried using:
<xsl:copy-of select="/csv-xml/record/csv-field-2/node()"/>
But the output is:
1234512345123451234512345
Code Used:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"
media-type="text/plain"/>
<xsl:template match="/">
<xsl:copy-of select="/csv-xml/record/csv-field-2/node()"/>
</xsl:template>
</xsl:stylesheet>
My expected result would be from the first 'csv-field-2' something like:
<name>12345</name>
My final goal is to be able to extract all the data needed from these XML's that may have more or less records using the same script. But that's a future problem.

Related

Concatenate XML in BPEL 2.0

Need your help for a requirement in BPEL 2.0. I have a collection in the below format
<FilesCollection>
<Files>
<transactionid>
<status>
<filename>
<Files>
<FilesCollection>
I would be getting several such collections while traversing through a ForEach loop.
Once I have exited the loop , I need to concatenate all the collections so that finally I get something as below
<FilesCollection>
<Files>
<transactionid>
<status>
<filename>
<Files>
<Files>
<transactionid>
<status>
<filename>
<Files>
<Files>
<transactionid>
<status>
<filename>
<Files>
<FilesCollection>
Please note that the number of FilesCollection element and the number of Files element appearing within it would be dynamic.
Please help me with this.
Thanks
Arijit
As i understand you have multiple FilesCollection in XML document and you want to wrap inside one then you need to do something like this:
Note: Suppose your root element is root in source XML
<?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="1.0">
<xsl:template match="root">
<root>
<FilesCollection>
<xsl:copy-of select="FilesCollection/node()"/>
</FilesCollection>
</root>
</xsl:template>
</xsl:stylesheet>

Group by ID and Sum Amount in XSLT

I'm fairly new to XSLT and stuck on a current problem. I've done some searches throughout Stackflow (seems like Muenchian method is the common group method) but I can't seem to mimic some of the posted ideas as of yet.
So I'm using a line item read system of which I'm trying to write code in XSLT to read every line to check if the supplier ID is the same, if true, it will aggregate this into one line, then sum the amounts. If not true, it should start a new line with the ID and sum the amount and so forth. I am using xml version='1.0'
Below is my current data file in XML:
<data>
<row>
<column1>06-11111</column1>
<column2>CP</column2>
<column3>744.04</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11111</column1>
<column2>CP</column2>
<column3>105.09</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11111</column1>
<column2>CP</column2>
<column3>1366.24</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11111</column1>
<column2>CP</column2>
<column3>485.71</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11112</column1>
<column2>Ever</column2>
<column3>459.60</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11112</column1>
<column2>Ever</column2>
<column3>409.14</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11112</column1>
<column2>Ever</column2>
<column3>397.12</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11113</column1>
<column2>GE</column2>
<column3>1425</column3>
<column4>CAD</column4>
</row>
<row>
<column1>06-11114</column1>
<column2>Husky</column2>
<column3>-215.14</column3>
<column4>USD</column4>
</row>
<row>
<column1>06-11114</column1>
<column2>Husky</column2>
<column3>2015</column3>
<column4>USD</column4>
</row>
<row>
<column1>06-11114</column1>
<column2>Husky</column2>
<column3>11195.34</column3>
<column4>USD</column4>
</row>
</data>
The output I would like to achieve after running the XSLT is
06-11111 | CP |2701.08
06-11112 | Ever |1265.86
06-11113 | GE |1425
06-11114 | Husky |12995.20
Any help to get me started would be fantastic!
Here is the grouping using the Muenchian method. I'll let you play with getting the numbers formatted correctly based on the number of decimal points.
I typically don't use this because it's limited, tricky and doesn't lend itself to push programming. But, it will work for you today.
<xsl:template match="#* | node()">
<xsl:apply-templates select="#* | node()"/>
</xsl:template>
<xsl:key name="rows" match="row" use="concat(column1, '||', column2)" />
<xsl:template match="data">
<xsl:for-each select="row[generate-id(.) = generate-id(key('rows', concat(column1, '||', column2))[1])]">
<xsl:sort select="column1" data-type="text" order="ascending"/>
<xsl:sort select="column2" data-type="text" order="ascending"/>
<xsl:value-of select="concat(column1,'|',column2,'|')"/>
<xsl:variable name="mySum">
<xsl:value-of select="sum(key('rows', concat(column1, '||', column2))/column3)"/>
</xsl:variable>
<xsl:value-of select="format-number($mySum,'#,##0.00')"/>
<xsl:value-of select="'
'"/>
</xsl:for-each>
</xsl:template>

XSLT: How can I identify the list of values are same?

I got the following xml snippet.
<Root>
<SomeTags></SomeTags>
<Values>
<dateFrom>date 1<dateFrom>
<dateEnd>date 2<dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
More Values here....
.....................
.....................
<Values>
<dateFrom>date n<dateFrom>
<dateEnd>date n+1<dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
</Root>
I want to compare all <value1> values and <value2> values. If they are same, I want to compress the whole as following:
If values1 == same in all Values and If values2== same in all Values then this should the output.
<Values>
<dateFrom>date 1<dateFrom> should be from the first Values item
<dateEnd>date n+1<dateEnd> should be from the last Values item
<value1>10</value1>
<value2>5</value2>
</Values>
Else
different template.
How can i achieve this in XSLT (1.0) in BizTalk 2009 ?
I know iteration. for-each. But is there a way to break as in procedural language. Can I somehow compare each value and return a boolean saying all values are same or not.. ??
Thank you all for your responses.
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Values">
<xsl:if test=
"not(preceding-sibling::Values
[value1=current()/value1 and value2=current()/value2])">
<Values>
<xsl:apply-templates select="value1|value2"/>
</Values>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the following well-formed XML document (produced from the severely malformed text that is provided in the question):
<t>
<Values>
<dateFrom>date 1</dateFrom>
<dateEnd>date 2</dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
<somethingElse/>
<Values>
<dateFrom>date n</dateFrom>
<dateEnd>date n+1</dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
</t>
produces the wanted result:
<t>
<Values>
<value1>10</value1>
<value2>5</value2>
</Values>
<somethingElse/>
</t>
Explanation:
Just applying the most fundamental design pattern of XSLT: Using and overriding the identity rule.

How to use parameter or variable values as node name?

I am trying to use the value of a parameter or variable as a node name inside a value-of select but so far failed..
So my XML is as below.
<Data>
<Name>John Smith</Name>
<Date>28112012</Date>
<Phone>iphone</Phone>
<Car>BMW</Car>
</Data>
And my incomplete xslt looks like below.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
exclude-result-prefixes="#all">
<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
<Output>
<xsl:value-of select="{$nodename}"/>
</Output>
</xsl:template>
</xsl:stylesheet>
Ideally I want the out put to be
<Output>John Smith</Output>
Is there any way I can do this using XSLT?
I want to be able to select appropriate node based on a users choice.
Thanks
SK
A wild guess, let me know if it works:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="#all">
<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
<Output>
<xsl:value-of select="//*[name()=$nodename]" />
</Output>
</xsl:template>
</xsl:stylesheet>

XSLT-1.0 can a variable be used to access to other nodes?

With a simple XML like this
<value>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</value>
I want to know if it is possible to access to a particular node from a node previously stored in a variable. The XSLT code is very short and explains better what I want to say
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node"/>
</root>
</xsl:template>
</xsl:stylesheet>
So I store the node in the variable "node". Then I can print the contents of the node with $node.
(EDIT) XML output
<root>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</root>
What I want to do is to print the contents of a sub-node, like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node/accession"/>
</root>
</xsl:template>
</xsl:stylesheet>
But it gives an error (Component returned failure code: 0x80600008 [nsIXSLTProcessor.transformToFragment]) (check here)
(EDIT) The XML that I would want is
<root>
<accession>111</accession>
</root>
NOTE: The question is not how can I get this output. The question is how, using a variable as in the XSLT provided, can I get this output.
(EDIT:SOLVED)
Actually it is possible, but as pointed out in the comments, the value of a variable has to be assigned with the "select" attribute if a node-set is required. So this code was not working since the variable had a tree fragment instead of a node-set stored in it (read more information here)
Thanks!
Try this:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value">
<root>
<xsl:for-each select="num">
<xsl:variable name="node" select="current()" />
<xsl:copy-of select="$node/accession" />
</xsl:for-each>
</root>
</xsl:template>
</xsl:transform>
Note that I used xsl:transform instead of xsl:stylesheet. Also, consider using version 2.0 instead of 1.0 if you have a compliant processor, it adds a lot of useful features.
I still don't see your need for a variable, though.