I don't have much knowledge on xslt . I am trying to remove some duplicate node from my input xml but the nodes are not exactly duplicate . The PremiseId value is duplicate but the latitude, Longitude, XCoordinate and YCoordinate values are different. If we get such data in our Input xml then we need to pick only that occurrence of XML node which has the latitude, Longitude values . Currently my xslt is only picking up the first occurrence of duplicate PremiseId.
Input XML :-
<CoordinateCollectionRes
xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema"
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
XSLT :-
<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<xsl:template match="/">
<CoordinateCollectionRes xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<xsl:for-each-group select="/ns1:CoordinateCollectionRes/ns1:ResponseParameter" group-by="./ns1:PremiseId">
<xsl:copy-of select=".[./ns1:PremiseId=current-grouping-key()]"/>
</xsl:for-each-group>
</CoordinateCollectionRes>
</xsl:template>
</xsl:stylesheet>
Output XML:-
<CoordinateCollectionRes xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema" xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
Expected Output XML :-
<CoordinateCollectionRes xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema" xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
How about:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema"
xpath-default-namespace="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/CoordinateCollectionRes">
<CoordinateCollectionRes>
<xsl:for-each-group select="ResponseParameter" group-by="PremiseId">
<xsl:copy-of select="(current-group()[string(Latitude|Latitutde)], current-group())[1]" copy-namespaces="no"/>
</xsl:for-each-group>
</CoordinateCollectionRes>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/93dFepY
Related
I wanna read the element count and apply the target xml element from a specific source element.
this is source xml to be read and counting POSEX field
<?xml version="1.0" encoding="UTF-8"?>
<INVOIC01>
<IDOC>
<POSEX>000010</POSEX>
<MENGE>1.000</MENGE>
<MENEE>EA</MENEE>
<GEWEI>KGM</GEWEI>
<BRGEW>13.000</BRGEW>
<PSTYV>TAN</PSTYV>
<WERKS>3000</WERKS>
</IDOC>
<IDOC>
<POSEX>000010</POSEX>
<MENGE>1.000</MENGE>
<MENEE>EA</MENEE>
<GEWEI>KGM</GEWEI>
<BRGEW>13.000</BRGEW>
<PSTYV>TAN</PSTYV>
<WERKS>3000</WERKS>
</IDOC>
</INVOIC01>
my xslt code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<!-- ============================================================================================= -->
<!-- Template: Enter the ordinal number in POSEX-->
<!-- ============================================================================================= -->
<xsl:template match="POSEX">
<D_6066>
<xsl:value-of select="count(preceding::POSEX)+1"/>
</D_6066>
</xsl:template>
</xsl:stylesheet>
outcome xml after run xslt code
<?xml version="1.0" encoding="UTF-8"?>
<D_6066>1</D_6066>
1.000
EA
KGM
13.000
TAN
3000
<D_6066>2</D_6066>
1.000
EA
KGM
13.000
TAN
3000
I want to expect target xml as below after run xslt.
<D_6066>2</D_6066>
If I understand correctly, you want your output document to consist of a single D_6066 element containing the count of the number of POSEX elements in the input document.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<D_6066>
<xsl:value-of select="count(//POSEX)"/>
</D_6066>
</xsl:template>
</xsl:stylesheet>
I am trying to get the output based on two separate nodes in XML using key concept of XSLT
I have below XML
<?xml version="1.0" encoding="UTF-8"?>
<mdti:Input xmlns:mdti="urn:com.workday/multiDocumentTransform/Input">
<mdti:Files xmlns:mdti="urn:com.workday/multiDocumentTransform/Input">
<mdti:EventFiles>
<mdti:File mdti:filename="first.xml" mdti:contentType="text/xml">
<wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
<wd:Report_Entry>
<wd:key>1234</wd:key>
<wd:comp>ABC</wd:comp>
<wd:asof>2021-03-24T04:59:32.179-07:00</wd:asof>
<wd:emplid>33333333</wd:emplid>
<wd:worker_type>EMP</wd:worker_type>
<wd:emp_type>Regular</wd:emp_type>
<wd:orig_hire_dt>2021-11-27</wd:orig_hire_dt>
<wd:rehire_dt>2019-04-01</wd:rehire_dt>
<wd:home_host_class>M</wd:home_host_class>
<wd:service_dt>2014-11-27</wd:service_dt>
</wd:Report_Entry>
</wd:Report_Data>
</mdti:File>
<mdti:File mdti:filename="second.xml" mdti:contentType="text/xml">
<wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
<wd:Report_Entry>
<wd:key>1234</wd:key>
<wd:supervisor_lname>xyz</wd:supervisor_lname>
<wd:hr_status>A</wd:hr_status>
<wd:hr_status_descr>Active</wd:hr_status_descr>
<wd:empl_status>A</wd:empl_status>
<wd:empl_status_descr>Active</wd:empl_status_descr>
<wd:ben_status>A</wd:ben_status>
<wd:home_address_change_dt>2019-07-30</wd:home_address_change_dt>
<wd:location>444</wd:location>
<wd:location_descr>Ind</wd:location_descr>
</wd:Report_Entry>
</wd:Report_Data>
</mdti:File>
</mdti:EventFiles>
</mdti:Files>
</mdti:Input>
**I am using below XSLT for my data. Please let me know if I am missing something and way to do it. The only identifier in each node is the mdti:filename **
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mdti="urn:com.workday/multiDocumentTransform/Input"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wd="urn:com.workday/bsvc"
exclude-result-prefixes="#all">
<xsl:key name="share" match="mdti:Input/mdti:Files/mdti:EventFiles/mdti:File/wd:Report_Data/wd:Report_Entry" use="mdti:Input/mdti:Files/mdti:EventFiles/mdti:File/wd:Report_Data/wd:Report_Entry/wd:key"/>
<xsl:template match="/">
<data>
<key>1234</key>
<xsl:copy-of select="key('share', wd:key)/wd:hr_status"/>
<emp_type>Regular</emp_type>
<supervisor_lname>xyz</supervisor_lname>
<hr_status>A</hr_status>
<location>444</location>
</data>
</xsl:template>
</xsl:stylesheet>
You have several mistakes.
First, <xsl:template match="/"> puts you in the context of the root node; from this context, the expression wd:key selects nothing - so your instruction <xsl:copy-of select="key('share', wd:key)/wd:hr_status"/> does nothing.
Next, if you want your key to match nodes in the second file, you should restrict it to match only nodes in the second file. Also, the use attribute must be relative to the matched node.
Furthermore, if you want the result to be in no-namespace, you cannot copy nodes from the input - at least not in XSLT 1.0.
There is more, but these are the ones that stand out immediately.
Consider the following example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mdti="urn:com.workday/multiDocumentTransform/Input"
xmlns:wd="urn:com.workday/bsvc"
exclude-result-prefixes="mdti wd">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="entry2" match="mdti:File[#mdti:filename='second.xml']/wd:Report_Data/wd:Report_Entry" use="wd:key"/>
<xsl:template match="/mdti:Input">
<root>
<xsl:for-each select="mdti:Files/mdti:EventFiles/mdti:File[#mdti:filename='first.xml']/wd:Report_Data/wd:Report_Entry">
<data>
<!-- data from file1 -->
<key>
<xsl:value-of select="wd:key"/>
</key>
<emplid>
<xsl:value-of select="wd:emplid"/>
</emplid>
<!-- data from file2 -->
<xsl:variable name="entry2" select="key('entry2', wd:key)" />
<supervisor_lname>
<xsl:value-of select="$entry2/wd:supervisor_lname"/>
</supervisor_lname>
<hr_status>
<xsl:value-of select="$entry2/wd:hr_status"/>
</hr_status>
</data>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, this will produce:
Result
<?xml version="1.0" encoding="utf-8"?>
<root>
<data>
<key>1234</key>
<emplid>33333333</emplid>
<supervisor_lname>xyz</supervisor_lname>
<hr_status>A</hr_status>
</data>
</root>
You can add fields from both branches as required.
I am trying to get the output based on two separate nodes in XML using key concept of XSLT
I have below XML
<?xml version="1.0" encoding="UTF-8"?>
<mdti:Input xmlns:mdti="urn:com.workday/multiDocumentTransform/Input">
<mdti:Files xmlns:mdti="urn:com.workday/multiDocumentTransform/Input">
<mdti:EventFiles>
<mdti:File mdti:filename="first.xml" mdti:contentType="text/xml">
<wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
<wd:Report_Entry>
<wd:key>1234</wd:key>
<wd:comp>ABC</wd:comp>
<wd:asof>2021-03-24T04:59:32.179-07:00</wd:asof>
<wd:emplid>33333333</wd:emplid>
<wd:worker_type>EMP</wd:worker_type>
<wd:emp_type>Regular</wd:emp_type>
<wd:orig_hire_dt>2021-11-27</wd:orig_hire_dt>
<wd:rehire_dt>2019-04-01</wd:rehire_dt>
<wd:home_host_class>M</wd:home_host_class>
<wd:service_dt>2014-11-27</wd:service_dt>
</wd:Report_Entry>
</wd:Report_Data>
</mdti:File>
<mdti:File mdti:filename="second.xml" mdti:contentType="text/xml">
<wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
<wd:Report_Entry>
<wd:key>1234</wd:key>
<wd:supervisor_lname>xyz</wd:supervisor_lname>
<wd:hr_status>A</wd:hr_status>
<wd:hr_status_descr>Active</wd:hr_status_descr>
<wd:empl_status>A</wd:empl_status>
<wd:empl_status_descr>Active</wd:empl_status_descr>
<wd:ben_status>A</wd:ben_status>
<wd:home_address_change_dt>2019-07-30</wd:home_address_change_dt>
<wd:location>444</wd:location>
<wd:location_descr>Ind</wd:location_descr>
</wd:Report_Entry>
</wd:Report_Data>
</mdti:File>
</mdti:EventFiles>
</mdti:Files>
</mdti:Input>
**I am using below XSLT for my data. Please let me know if I am missing something and way to do it. The only identifier in each node is the mdti:filename **
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mdti="urn:com.workday/multiDocumentTransform/Input"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wd="urn:com.workday/bsvc"
exclude-result-prefixes="#all">
<xsl:key name="share" match="mdti:Input/mdti:Files/mdti:EventFiles/mdti:File/wd:Report_Data/wd:Report_Entry" use="mdti:Input/mdti:Files/mdti:EventFiles/mdti:File/wd:Report_Data/wd:Report_Entry/wd:key"/>
<xsl:template match="/">
<data>
<key>1234</key>
<xsl:copy-of select="key('share', wd:key)/wd:hr_status"/>
<emp_type>Regular</emp_type>
<supervisor_lname>xyz</supervisor_lname>
<hr_status>A</hr_status>
<location>444</location>
</data>
</xsl:template>
</xsl:stylesheet>
You have several mistakes.
First, <xsl:template match="/"> puts you in the context of the root node; from this context, the expression wd:key selects nothing - so your instruction <xsl:copy-of select="key('share', wd:key)/wd:hr_status"/> does nothing.
Next, if you want your key to match nodes in the second file, you should restrict it to match only nodes in the second file. Also, the use attribute must be relative to the matched node.
Furthermore, if you want the result to be in no-namespace, you cannot copy nodes from the input - at least not in XSLT 1.0.
There is more, but these are the ones that stand out immediately.
Consider the following example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mdti="urn:com.workday/multiDocumentTransform/Input"
xmlns:wd="urn:com.workday/bsvc"
exclude-result-prefixes="mdti wd">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="entry2" match="mdti:File[#mdti:filename='second.xml']/wd:Report_Data/wd:Report_Entry" use="wd:key"/>
<xsl:template match="/mdti:Input">
<root>
<xsl:for-each select="mdti:Files/mdti:EventFiles/mdti:File[#mdti:filename='first.xml']/wd:Report_Data/wd:Report_Entry">
<data>
<!-- data from file1 -->
<key>
<xsl:value-of select="wd:key"/>
</key>
<emplid>
<xsl:value-of select="wd:emplid"/>
</emplid>
<!-- data from file2 -->
<xsl:variable name="entry2" select="key('entry2', wd:key)" />
<supervisor_lname>
<xsl:value-of select="$entry2/wd:supervisor_lname"/>
</supervisor_lname>
<hr_status>
<xsl:value-of select="$entry2/wd:hr_status"/>
</hr_status>
</data>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, this will produce:
Result
<?xml version="1.0" encoding="utf-8"?>
<root>
<data>
<key>1234</key>
<emplid>33333333</emplid>
<supervisor_lname>xyz</supervisor_lname>
<hr_status>A</hr_status>
</data>
</root>
You can add fields from both branches as required.
I have the XML structure as below
I am deep down in the inner elements of env:Envelope and want to jump to something in Report_Entry.
I tried multiple paths like *, .., . and more but was not able to get to the expected node.
The <xsl:for-each select="/*"> is taking me to the root but <xsl:for-each select="/*/wd:Report_Entry"> does NOT work
<root>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<Child1>
<Child2>
<Child3>
</Chiild3>
</Child2>
</Child1>
</env:Envelope>
<wd:Report_Entry xmlns:wd="urn:com.workday.report/CR_INT001_Worker_Benefit_Group>
<Child4>
<Child5>
</Child5>
</Child4>
</wd:Report_Entry>
</root>
Here are the XSLT declarations
<xsl:stylesheet version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.workday/bsvc"
xmlns:out="http://www.workday.com/integration/output"
xmlns:func="http://www.func.com"
xmlns:xtt="urn:com.workday/xtt"
xmlns:env="urn:com.workday/bsvc" wd:version="v35.0"
exclude-result-prefixes="xs wd out xsl xtt func">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<Root>
<-- I am in Chiild 3 and want to retrieve the value from Child 5-->
</Root>
</xsl:template>
</xsl:stylesheet>
Any help appreciated
How to visualize the following XML
<Root>
<name1>
<node1> some text </node1>
<node2> <node22/> <node23/> </node2>
</name1>
<name2>
<node1> some text </node1>
<node2> <node22/> <node23/> </node2>
</name2>
</Root>
The name of these node1, node12, node13, Root nodes are known, but name1, name2, etc. are unknown in advance.
The desired output should be a set of two tables for each name, one for node1 and other for node2.
My problem is that I can't iterate over nameX because I don't know the exact name of the node. In the example above I used nameX but it can be any valid name.
The desired output should be a set of two tables for each name, one
for node1 and other for node2.
Try it this way:
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="/Root">
<xsl:copy>
<xsl:for-each select="*">
<table>
<!-- build your table here -->
</table>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>