Populating nodes based on condition in Receiver file - node.js

I need to populate one segment based on input values.the requirement like below
in the input payload we are getting below segment like
<charac>
<charactername>
<charactervalue>
</charac>
so the above segment may come multiple times but based on the few values only need to populate the segment for example,
<charac>
<charactername>print</charactername>
<charactervalue>123</charactervalue>
</charac>
<charac>
<charactername>comp</charactername>
<charactervalue>1234</charactervalue>
</charac>
<charac>
<charactername>pal</charactername>
<charactervalue>1235</charactervalue>
</charac>
so here only I need populate segment when charactername is equal to only print or comp
the receiver structure the segment is
<e1edl1>
<at>
<rt>
</e1edl1>
so the output should be like
<e1edl1>
<at>print</at>
<rt>123</rt>
</e1edl1>
<e1edl1>
<at>comp</at>
<rt>1234</rt>
</e1edl1>
I tried with below code
<ns0:if test="count(./charac)!=0">
<ns0:for-each select="./charac">
<e1edl1 SEGMENT="1">
<at>
<ns0:value-of select="charactername" />
</at>
<rt>
<ns0:value-of select="charactervalue" />
</rt>
</e1edl1>
</ns0:for-each>
</ns0:if>
could you please help on this.
Regards,
Janardhan

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//charac[charactername='print' or charactername='comp']" />
</xsl:template>
<xsl:template match="charac">
<e1edl1>
<at><xsl:value-of select="charactername" /></at>
<rt><xsl:value-of select="charactervalue" /></rt>
</e1edl1>
</xsl:template>
</xsl:stylesheet>

Related

How to return the count of input that start with a string in XSL?

I'm trying to return the count of names which start with NB but im having trouble as it returns 0. I believe i got the syntax right so it's kind of frustrating to see it return 0. Any help would be appreciated!
XML Input:
<?xml version='1.1' encoding='UTF-8'?>
<breaches>
<breach>
<name>NB111</name>
<severity>MAJOR</severity>
</breach>
<breach>
<name>NB222</name>
<severity>MAJOR</severity>
</breach>
<breach>
<name>NB333</name>
<severity>MAJOR</severity>
</breach>
<breach>
<name>PO999</name>
<severity>MAJOR</severity>
</breach>
</breaches>
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(/breaches/breach/name/*[contains(text(), 'NB')])" />
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>0
I'm trying to return the count of names which start with NB
If you want to count names that start with "NB" then use the starts-with() function, not the contains() function:
<xsl:value-of select="count(breaches/breach[starts-with(name, 'NB')])"/>
This actually counts breach elements that have a child name element that starts with "NB". IOW, it assumes each breach has only one name. If such assumption is incorrect, then use:
<xsl:value-of select="count(breaches/breach/name[starts-with(., 'NB')])"/>
to count the actual name elements.
You could do it like this :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(breaches/breach/name[contains(., 'NB')])" />
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/jxWZS86

XSL loop through faultblock and apppend values to a string without using template

How to loop through fault_block and append values to a string/variable without using template in XSLT. fault_block may occur once or twice or n number of times based on validation errors
Desired Output: 11-Invalid ID;22-Invalid Password;.....nn-Error;
<status>
<code>00</code>
<description>Success</description>
<faultblock>
<faultcode>11</faultcode>
<faultdesc>Invalid ID</faultdesc>
</faultblock>
<faultblock>
<faultcode>22</faultcode>
<faultdesc>Invalid Password</faultdesc>
</faultblock>
<faultblock>
<faultcode>nn</faultcode>
<faultdesc>Error</faultdesc>
</faultblock>
</status>
I'm guessing you want to get your output using only a for-each statement instead of using multiple templates.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//faultblock">
<xsl:value-of select="faultcode"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="faultdesc"/>
<xsl:text>;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
See it working here: https://xsltfiddle.liberty-development.net/6pS2B71

How to process two parts of a single xml by streaming?

I have a really large XML that has two different sets of data. I need to stream through one and do a lookup on the other. My Data looks like below
<?xml version="1.0" encoding="utf-8"?>
<Root>
<EmployeePersonal>
<Employee>
<ID>21234</ID>
<Name>Jim Carrey</Name>
<Age>43</Age>
<City>Chicago</City>
<State>IL</State>
</Employee>
<Employee>
<ID>41876</ID>
<Name>Edward Norton</Name>
<Age>33</Age>
<City>New York</City>
<State>NY</State>
</Employee>
<Employee>
<ID>51239</ID>
<Name>Eli Roth</Name>
<Age>46</Age>
<City>Los Angeles</City>
<State>CA</State>
</Employee>
</EmployeePersonal>
<EmployeeEmployment>
<Empl>
<Emplid>21234</Emplid>
<Title>HR Partner</Title>
<HireDate>2008-12-29</HireDate>
</Empl>
<Empl>
<Emplid>41876</Emplid>
<Title>Comp Partner</Title>
<HireDate>1999-07-09</HireDate>
</Empl>
<Empl>
<Emplid>51239</Emplid>
<Title>Programmer</Title>
<HireDate>2004-12-06</HireDate>
</Empl>
</EmployeeEmployment>
</Root>
I would like to loop through the /Root/EmployeePersonal data and lookup the /Root/EmployeeEmployment by matching on the employee id.
I tried to loop through one and then load into a map and then loop through the other, but kept getting an error. Finally I tried loading one set into a variable and then tried to stream the other, but in vain. This is what I've tried so far.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:mode streamable="yes" on-no-match="shallow-skip"/>
<xsl:output indent="no"/>
<xsl:variable name="vEmploymentData" select="Root/copy-of(EmployeeEmployment)"/>
<xsl:template match="/Root/EmployeePersonal">
<AllEmployeeData>
<xsl:for-each select="Employee/copy-of()">
<Employee>
<xsl:copy-of select="./*"/>
<xsl:copy-of select="$vEmploymentData/Empl[Emplid=current()/ID]/*[local-name() ne 'Emplid']"/>
</Employee>
</xsl:for-each>
</AllEmployeeData>
</xsl:template>
</xsl:stylesheet>
I need to merge these two sets of data together. Since the data is huge, is there a way to stream both sets of data in?
Thanks
Given that you need to different sets of the data at the same time to compare and merge them I am not sure there is an easy single pass, streamable and therefore forwards-only approach, so some easy way around that would be to set up the same document twice as an xsl:merge-source for an xsl:merge:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:param name="input-uri" as="xs:string" select="'input1.xml'"/>
<xsl:output indent="yes"/>
<xsl:template name="xsl:initial-template">
<AllEmployeeData>
<xsl:merge>
<xsl:merge-source name="emp-details1" for-each-source="$input-uri" streamable="yes" select="Root/EmployeePersonal/Employee">
<xsl:merge-key select="ID"/>
</xsl:merge-source>
<xsl:merge-source name="emp-details2" for-each-source="$input-uri" streamable="yes" select="Root/EmployeeEmployment/Empl">
<xsl:merge-key select="Emplid"/>
</xsl:merge-source>
<xsl:merge-action>
<xsl:copy>
<xsl:copy-of select="current-merge-group('emp-details1')/*, current-merge-group('emp-details2')/(* except Emplid)"/>
</xsl:copy>
</xsl:merge-action>
</xsl:merge>
</AllEmployeeData>
</xsl:template>
</xsl:stylesheet>
As xsl:merge works with a snapshot, that has the advantage that the access to ID and Emplid child is not a problem. You also don't have to take any particular action to buffer data in maps or accumulators. The main disadvantage is that xsl:merge expects the sources to be ordered by the merge keys, that seems to be the case for your sample data but I am not sure that will be true for your complete data.
You would run such a stylesheet with Saxon 9 EE using the command line option -it to start without a primary input but with the initial template named xsl:initial-template instead.
As an alternative approach that only reads through the main input once but buffers the EmployeePersonal/Employee in an accumulator using a Saxon 9.9 EE specific saxon:capture attribute you could use:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
exclude-result-prefixes="#all">
<xsl:output indent="yes"/>
<xsl:mode streamable="yes" on-no-match="shallow-skip" use-accumulators="emp-details1"/>
<xsl:accumulator name="emp-details1" streamable="yes" as="element(Employee)*" initial-value="()">
<xsl:accumulator-rule match="EmployeePersonal/Employee" phase="end" saxon:capture="yes" select="$value, ."/>
</xsl:accumulator>
<xsl:template match="/*">
<AllEmployeeData>
<xsl:apply-templates/>
</AllEmployeeData>
</xsl:template>
<xsl:template match="EmployeeEmployment/Empl">
<Employee>
<xsl:variable name="this" select="copy-of()"/>
<xsl:copy-of select="accumulator-before('emp-details1')[ID = $this/Emplid]/*, $this/(* except Emplid)"/>
</Employee>
</xsl:template>
</xsl:stylesheet>
This reads through the input only once but buffers all EmployeePersonal/Employee in memory and then uses the buffered/accumulated elements later one when EmployeeEmployment/Empl is matched to output the necessary corresponding data. This also of course, as the output is created when matching EmployeeEmployment/Empl, requires the presence of all ids in that data, I am not sure that is the case or whether the EmployeePersonal/Employee is the main input and you only look for matching EmployeeEmployment/Empl if it exists.
Finally, you could as well try to store the details of EmployeePersonal/Employee in an accumulator using a map and then access this from the template for EmployeeEmployment/Empl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:saxon="http://saxon.sf.net/"
expand-text="yes"
exclude-result-prefixes="#all">
<xsl:output indent="yes"/>
<xsl:mode streamable="yes" on-no-match="shallow-skip" use-accumulators="#all"/>
<xsl:accumulator name="current-emp-id" streamable="yes" as="xs:integer?" initial-value="()">
<xsl:accumulator-rule match="EmployeePersonal/Employee/ID/text()" select="xs:integer(.)"/>
</xsl:accumulator>
<xsl:accumulator name="emp-details" streamable="yes" as="map(xs:integer, map(xs:string, xs:string))" initial-value="map{}">
<xsl:accumulator-rule match="EmployeePersonal/Employee/ID/text()" select="map:put($value, xs:integer(.), map{})"/>
<xsl:accumulator-rule match="EmployeePersonal/Employee/*[not(self::ID)]/text()"
select="map:put($value, accumulator-before('current-emp-id'), map:put(map:get($value, accumulator-before('current-emp-id')), local-name(..), string()))"/>
</xsl:accumulator>
<xsl:template match="/*">
<AllEmployeeData>
<xsl:apply-templates/>
</AllEmployeeData>
</xsl:template>
<xsl:template match="EmployeeEmployment/Empl">
<Employee>
<xsl:variable name="this" select="copy-of()"/>
<ID>{$this/Emplid}</ID>
<xsl:apply-templates select="map:get(accumulator-before('emp-details'), xs:integer($this/Emplid))" mode="entry-to-element"/>
<xsl:copy-of select="$this/(* except Emplid)"/>
</Employee>
</xsl:template>
<xsl:template match=".[. instance of map(*)]" mode="entry-to-element">
<xsl:variable name="map" select="."/>
<xsl:for-each select="map:keys(.)">
<xsl:element name="{.}">{map:get($map, .)}</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Of course it will still buffer all the EmployeePersonal/Employee data, only this time using a light-weight map instead of a sequence of XML snapshot elements. The main disadvantage is that XSLT 3/XPath 3.1 maps have no order so unless you later spell out each entry you are looking for in the wanted order you get the results in a random order. In the example above I have simply used a for-each to convert map entries back to XML elements so the order is a random order and not the one of the input XML (that the xsl:merge approach or the saxon:capture approach would preserve).
As you asked in a comment about using xsl:merge but provide the input as a primary input source, the only way I can think of is to use it as a dummy streamable input and then to provide its document-uri() for the xsl:merge/xsl:merge-source, that is, to adapt the code from above to
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:mode streamable="yes"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<AllEmployeeData>
<xsl:variable name="input-uri" as="xs:anyURI" select="document-uri()"/>
<xsl:message select="$input-uri"/>
<xsl:merge>
<xsl:merge-source name="emp-details1" for-each-source="$input-uri" streamable="yes" select="Root/EmployeePersonal/Employee">
<xsl:merge-key select="ID"/>
</xsl:merge-source>
<xsl:merge-source name="emp-details2" for-each-source="$input-uri" streamable="yes" select="Root/EmployeeEmployment/Empl">
<xsl:merge-key select="Emplid"/>
</xsl:merge-source>
<xsl:merge-action>
<xsl:copy>
<xsl:copy-of select="current-merge-group('emp-details1')/*, current-merge-group('emp-details2')/(* except Emplid)"/>
</xsl:copy>
</xsl:merge-action>
</xsl:merge>
</AllEmployeeData>
</xsl:template>
</xsl:stylesheet>
Interestingly enough, that works with Saxon 9.7 EE (of course that opening the same input three times for streaming) but fails with an error " XTSE3430: Template rule is not streamable * The merge-source/#select expression is not striding" in Saxon 9.8 and 9.9 EE.
Not sure whether that helps, in earlier questions you seemed to indicate that the tool you use embeds Saxon 9.7 EE so it might be an option.

from string to number xslt

I want to make a comparison of two different date’s. To realize that, I want to transform to number.
Input example:
<file>
<date>2015-11-06 09:00/>
</file>
<history>
<date>2016-01-12 10:00/>
</history>
First I extract the time, from date. And put the result in a var.
<xsl:for-each select="//item/metadata/document/file/date">
<xsl:variable name="d_log"
select="substring-before(., ' ')" as="xs:string"/>
So the value of the variable would be, for example 2015-11-06.
The next step is, that I want to transform 2015-11-06 in to 20151106.
Questions is how I can transform from a string to a number?
Or is there a easier way?
You can do:
translate(substring-before(., ' '), '-', '')
to get the expected number.
However, your syntax suggests you are using XSLT 2.0. If so, why don't you convert the given strings to xs:date or xs:dateTime and compare them as such?
Here is my result.
<?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:output method="xml" indent="yes"></xsl:output>
<xsl:template match="/">
<aaa>
<xsl:for-each select="//item/metadata/document/file/date">
<xsl:variable name="d_log"
select="translate(substring-before(., ' '), '-', '')"
as="xs:string"/>
<xsl:for-each select="../../history/log/date">
<xsl:variable name="d_doc" select="translate(substring-before(., ' '), '-', '')" as="xs:string"/>
<xsl:if test="$d_doc > $d_log">
<bb>
<xsl:value-of select="node()"/>
</bb>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</aaa>
</xsl:template>
</xsl:stylesheet>

XSLT finding matching node by comparing element text to substring result

I am processing this XML:
<Brand>
<Brand_Name>BLENDERM</Brand_Name>
<Brand_Code>1103</Brand_Code>
<Groups>
<Group>
<Group_Code>657</Group_Code>
<Parent_Code>0</Parent_Code>
<Group_Level>1</Group_Level>
<Group_Name>Brand Default</Group_Name>
<Product>
<Pip_code>0032359</Pip_code>
<Status>In Use</Status>
Using this XSLT:
<xsl:template match="Product" mode="phase-3">
<xsl:value-of select="document('rx_catmapping.xml')/descendant::mapping[source=substring(ancestor::Brand/Brand_Name,1,1)]/target"/>
</xsl:template>
Here is a sample of rx_catmapping.xml:
<Lookup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<mapping>
<source>a</source>
<target>788</target>
</mapping>
<mapping>
<source>B</source>
<target>789</target>
</mapping>
</Lookup>
So, I am processing the Product element, which is a descendant of Brand. The first letter of Brand/Brand_Name in this case is B, and I am trying to output the value 789 by looking it up in rx_catmapping.xml. This should be really simple but I am completely stumped! I have tried changing the first part of the XPath to refer to document('rx_catmapping.xml')/Lookup/mapping, or document('rx_catmapping.xml')//mapping. I have also tried changing the first half of the comparison to string(source), or to source/text(), but neither of these works either. (The reason for trying this was that using source='B', for example, did seem to work, so I wondered if I was trying to compare two incompatible data types.)
Thanks in advance for your help.
Define a key
<xsl:key name="k1" match="mapping" use="source"/>
then use
<xsl:variable name="map-doc" select="document('rx_catmapping.xml')"/>
and
<xsl:variable name="letter" select="substring(ancestor::Brand/Brand_Name,1,1)"/>
<xsl:for-each select="$map-doc">
<xsl:value-of select="key('k1', $letter)/target"/>
</xsl:for-each>
With XSLT 2.0 you can simplify that to
<xsl:value-of select="key('k1', substring(ancestor::Brand/Brand_Name,1,1), $map-doc)"/>
The problem is, I believe, that at the point where you do ancestor::Brand/Brand_Name, the context is the mapping element in the external file. This worked for me
<xsl:template match="/">
<xsl:apply-templates select=".//Product"/>
</xsl:template>
<xsl:template match="Product">
<xsl:variable name="x" select="substring(ancestor::Brand/Brand_Name,1,1)"/>
<xsl:value-of select="document('file:///c:/temp/rx_catmapping.xml')//mapping[source=$x]/target"/>
</xsl:template>

Resources