how to match the attribute value to another attribute - xslt-1.0

In the xml file the history element contains two ms_id elements. I want to match article_set/article in attribute of "ms_on" value to the history/ms_id attribute of "ms_no" and view in browser.
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<!--s1.dtd v4.20-->
<!DOCTYPE article_set SYSTEM "s1.dtd">
<article_set dtd_version="4.20">
<article export_date="2016-12-27 00:00:00.0" lang="EN" ms_no="EMI-2016-1600.R1" rev="1">
<history>
<ms_id ms_no="EMI-2016-1600.R1">
<rev_id>1</rev_id>
<submitted_date>
<year>2016</year>
<month>12</month>
<day>23</day>
<hour>13</hour>
<minute>02</minute>
<second>36</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</submitted_date>
<received_date>
<year>2016</year>
<month>10</month>
<day>17</day>
<hour>05</hour>
<minute>59</minute>
<second>50</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</received_date>
<received_date_resub>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</received_date_resub>
<revised_date>
<year>2016</year>
<month>12</month>
<day>23</day>
<hour>13</hour>
<minute>02</minute>
<second>36</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</revised_date>
<decision_date>
<year>2016</year>
<month>12</month>
<day>27</day>
<hour>03</hour>
<minute>44</minute>
<second>07</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</decision_date>Kindly check and do the needful.
<approval_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</approval_date>
<author_returned_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</author_returned_date>
<most_recent_decision_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</most_recent_decision_date>
<transmission_date>
<year>2016</year>
<month>10</month>
<day>17</day>
<hour>05</hour>
<minute>59</minute>
<second>50</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</transmission_date>
<web_publish_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</web_publish_date>
<task name="Forward to Admin" task_id="50995" type="forward document">
<status doc_task_id="206939948" event_date="2016-12-23T01:02:37" s1_staff="false" user_id="4799597" user_role="Admin">completed</status>
<assigned date="2016-12-23T01:02:37" role="Admin" user_id="4799597"/>
</task>
<task name="Branch" task_id="51104" type="branch">
<status doc_task_id="206939949" event_date="2016-12-23T01:02:37" s1_staff="false" user_id="0" user_role="SYSTEM ROLE">completed</status>
</task>
<task name="Forward to Juan Luis Ramos" task_id="173153" type="forward document">
<status doc_task_id="206939950" event_date="2016-12-23T01:02:37" s1_staff="false" user_id="6603746" user_role="Editor">completed</status>
<assigned date="2016-12-23T01:02:37" role="Editor" user_id="6603746"/>
</task>
<task name="ED Checklist" task_id="51108" type="manuscript checklist">
<status doc_task_id="206939951" event_date="2016-12-24T03:20:35" s1_staff="false" user_id="6603746" user_role="Editor">completed</status>
</task>
<task name="ED Decision" task_id="50993" type="decision">
<status doc_task_id="206939952" event_date="2016-12-27T03:44:07" s1_staff="false" user_id="6603746" user_role="Editor">abandoned</status>
</task>
<task name="ED Pick Reviewers" task_id="50987" type="pick2">
<status doc_task_id="206961933" event_date="2016-12-26T12:04:53" s1_staff="false" user_id="6603746" user_role="Editor">completed</status>
</task>
<task name="ED Invite Reviewers" task_id="146873" type="invite">
<status doc_task_id="206961934" event_date="2016-12-26T12:04:53" s1_staff="false" user_id="6603746" user_role="Editor">completed</status>
</task>
<task name="ED Decision" task_id="50993" type="decision">
<status doc_task_id="207034905" event_date="2016-12-27T03:44:07" s1_staff="false" user_id="6603746" user_role="Editor">completed</status>
<decision decision_name="Accept" decision_type="Accept"/>
</task>
<task name="Production Checklist" task_id="73019" type="production checklilst">
<status doc_task_id="207063310" event_date="2016-12-27T03:44:37" s1_staff="false" user_id="6603746" user_role="Editor">completed</status>
</task>
<task name="Production Checklist" task_id="73019" type="production checklilst">
<status doc_task_id="207063311" event_date="2016-12-27T03:44:37" s1_staff="false" user_id="0" user_role="">abandoned</status>
</task>
<task name="Branch on Batch Destination" task_id="194394" type="branch">
<status doc_task_id="207063343" event_date="2016-12-27T03:44:37" s1_staff="false" user_id="0" user_role="SYSTEM ROLE">completed</status>
</task>
<task name="Assign to Batch EMI" task_id="51001" type="assign to batch">
<status doc_task_id="207063344" event_date="2016-12-27T03:44:37" s1_staff="false" user_id="0" user_role="Production Editor">processing</status>
</task>
</ms_id>
<ms_id ms_no="EMI-2016-1600">
<rev_id>0</rev_id>
<submitted_date>
<year>2016</year>
<month>10</month>
<day>17</day>
<hour>05</hour>
<minute>59</minute>
<second>50</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</submitted_date>
<received_date>
<year>2016</year>
<month>10</month>
<day>17</day>
<hour>05</hour>
<minute>59</minute>
<second>50</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</received_date>
<received_date_resub>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</received_date_resub>
<revised_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</revised_date>
<decision_date>
<year>2016</year>
<month>11</month>
<day>20</day>
<hour>16</hour>
<minute>51</minute>
<second>51</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</decision_date>
<approval_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</approval_date>
<author_returned_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</author_returned_date>
<most_recent_decision_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</most_recent_decision_date>
<transmission_date>
<year>2016</year>
<month>10</month>
<day>17</day>
<hour>05</hour>
<minute>59</minute>
<second>50</second>
<time_zone>(GMT-05:00) Eastern Time (US & Canada)</time_zone>
</transmission_date>
<web_publish_date>
<year/>
<month/>
<day/>
<hour/>
<minute/>
<second/>
<time_zone/>
</web_publish_date>
</ms_id>
</history>
</article>
</article_set>
XSLT File:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<style type="text/css">
</style>
</head>
<body style="font-family:Arial;">
<h2>Wiley Meta Information</h2>
<xsl:for-each select="article_set/article">
<p><b>Article Title:</b> <xsl:value-of select="article_title"/></p>
<xsl:for-each select="author_list/author">
<p><b>Authors:</b> <span style="color:blue;font-weight:bold;" title="salutation"><xsl:value-of select="salutation"/></span> <span style="color:green;font-weight:bold;" title="first name"><xsl:value-of select="first_name"/></span> <span style="color:skyblue;font-weight:bold;" title="middle name"><xsl:value-of select="middle_name"/></span> <span style="color:brown;font-weight:bold;" title="last name"><xsl:value-of select="last_name"/></span></p>
<p><b>ORC ID:</b> <xsl:value-of select="orcid"/></p>
</xsl:for-each>
<p><b>Corresponding Author:</b> <xsl:value-of select="corresponding_author"/></p>
<xsl:for-each select="history/ms_id/submitted_date">
<p><b>Accepted Date:</b> <xsl:value-of select="day"/>-<xsl:value-of select="month"/>-<xsl:value-of select="year"/></p>
</xsl:for-each>
<xsl:for-each select="history/ms_id/received_date">
<p><b>Received Date:</b> <xsl:value-of select="day"/>-<xsl:value-of select="month"/>-<xsl:value-of select="year"/></p>
</xsl:for-each>
<xsl:for-each select="history/ms_id/revised_date">
<p><b>Revised Date:</b> <xsl:value-of select="day"/>-<xsl:value-of select="month"/>-<xsl:value-of select="year"/></p>
</xsl:for-each></xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

The question is somewhat confusing, because the provided XML has no article or author data.
To concentrate on your specific problem:
I want to match article_set/article in attribute of "ms_on" value to
the history/ms_id attribute of "ms_no"
the best way to resolve cross-references is by using a key. Consider the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="rev" match="ms_id" use="#ms_no" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="article_set/article">
<p>Article information goes here</p>
<p>Author/s information goes here</p>
<!-- get data about revision dates -->
<xsl:apply-templates select="key('rev', #ms_no)"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="ms_id">
<xsl:for-each select="submitted_date">
<p>
<b>Accepted Date:</b>
<xsl:text> </xsl:text>
<xsl:value-of select="concat(day, '-', month, '-', year)"/>
</p>
</xsl:for-each>
<xsl:for-each select="received_date">
<p>
<b>Received Date:</b>
<xsl:text> </xsl:text>
<xsl:value-of select="concat(day, '-', month, '-', year)"/>
</p>
</xsl:for-each>
<xsl:for-each select="revised_date">
<p>
<b>Revised Date:</b>
<xsl:text> </xsl:text>
<xsl:value-of select="concat(day, '-', month, '-', year)"/>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result will be:
<html>
<body>
<p>Article information goes here</p>
<p>Author/s information goes here</p>
<p>
<b>Accepted Date:</b> 23-12-2016</p>
<p>
<b>Received Date:</b> 17-10-2016</p>
<p>
<b>Revised Date:</b> 23-12-2016</p>
</body>
</html>

Related

Find distinct values across each node in xslt1.0

I want to find distinct values across each node. but when i use muenchian method it will select distinct values only at top level.
Currently i am getting output as:
Bag 20
Tray 30
But i want to group type and quantity for each item
<!--for 1st item-->
Bag 20
Tray 30
<!--for 2nd item-->
Bag 20
Box 20
Tray 30
I am doing some research to make it work but did not succeed.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<Items>
<Item>
<name>A</name>
<type>Bag</type>
<input quantity="20"/>
</Item>
<Item>
<name>A</name>
<type>Bag</type>
<input quantity="20"/>
</Item>
<Item>
<name>B</name>
<type>Metal</type>
<input quantity="20"/>
</Item>
<Item>
<name>A</name>
<type>Tray</type>
<input quantity="30"/>
</Item>
</Items>
<Items>
<Item>
<name>A</name>
<type>Bag</type>
<input quantity="20"/>
</Item>
<Item>
<name>A</name>
<type>Box</type>
<input quantity="20"/>
</Item>
<Item>
<name>B</name>
<type>Metal</type>
<input quantity="20"/>
</Item>
<Item>
<name>A</name>
<type>Tray</type>
<input quantity="30"/>
</Item>
</Items>
</Test>
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl"
extension-element-prefixes="date"
>
<xsl:key name="item-key" match="Items/Item[name='A']" use="input/#quantity" />
<xsl:template match="/" name="Barcode">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="ManufacturLabelSize-first" page-height="297mm" page-width="210mm" margin-top="25.4mm" margin-right="25.4mm" margin-left="25.4mm" margin-bottom="25.4mm">
<fo:region-body margin-top="15mm" />
<fo:region-before />
<fo:region-after />
</fo:simple-page-master>
<fo:simple-page-master master-name="ManufacturLabelSize-rest" page-height="297mm" page-width="210mm" margin-top="25.4mm" margin-right="25.4mm" margin-left="25.4mm" margin-bottom="25.4mm">
<fo:region-body margin-top="15mm"/>
<fo:region-before />
<fo:region-after />
</fo:simple-page-master>
<fo:page-sequence-master master-name="ManufacturLabelSize-portrait">
<fo:repeatable-page-master-alternatives>
<fo:conditional-page-master-reference master-reference="ManufacturLabelSize-first"
page-position="first"/>
<fo:conditional-page-master-reference master-reference="ManufacturLabelSize-rest"
page-position="rest"/>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="ManufacturLabelSize-portrait" id="pSeqID">
<fo:flow flow-name="xsl-region-body">
<fo:table >
<fo:table-body border="solid" border-width="0.5pt">
<fo:table-row>
<fo:table-cell>
<fo:block>
<xsl:for-each select="Test">
<xsl:for-each select="Items">
<xsl:for-each select="Item[name='A'][count(. | key('item-key',input/#quantity)[1])=1]">
<fo:block>
<xsl:value-of select="type"/> <fo:inline color="white">XXX</fo:inline><xsl:value-of select="input/#quantity"/>
</fo:block>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
Appreciate your help!
If - as it seems - you want to find distinct Items whose name is A by their type separately for each Items, then you need to include the parent Items' id in the key. Here's a simplified example:
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:key name="k" match="Item[name='A']" use="concat(type, '|', generate-id(..))"/>
<xsl:template match="/Test">
<root>
<xsl:for-each select="Items">
<xsl:comment>
<xsl:value-of select="position()" />
</xsl:comment>
<xsl:for-each select="Item[name='A'][count(. | key('k', concat(type, '|', generate-id(..)))[1]) = 1]">
<item>
<type>
<xsl:value-of select="type"/>
</type>
<quantity>
<xsl:value-of select="input/#quantity"/>
</quantity>
</item>
</xsl:for-each>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result will be:
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<!--1-->
<item>
<type>Bag</type>
<quantity>20</quantity>
</item>
<item>
<type>Tray</type>
<quantity>30</quantity>
</item>
<!--2-->
<item>
<type>Bag</type>
<quantity>20</quantity>
</item>
<item>
<type>Box</type>
<quantity>20</quantity>
</item>
<item>
<type>Tray</type>
<quantity>30</quantity>
</item>
</root>

Replacing a child node with another node without affecting its sub node

I need to replace the child node with another node without affecting its sub nodes, I tried matching the child node but was unable to
This is the xml format
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>{A124-B421-C325-D467}</MessageId>
<Action>find</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Run xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Run">
<RunObject class="entity">
<A1>NA</A1>
<A2>False</A2>
<Object class="entity">
<A3>02</A3>
</Object>
<A4>ER</A4>
</RunObject>
</Run>
</MessageParts>
</Body>
</Envelope>
This is the xml format that i require
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>{A124-B421-C325-D467}</MessageId>
<Action>find</Action>
</Header>
<Body>
<Document>
<Item>
<A1>NA</A1>
<A2>False</A2>
<Base>
<A3>02</A3>
</Base>
<A4>ER</A4>
</Item>
</Document>
</Body>
</Envelope>
This is the code that i through which i tried to change the format
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:r="http://schemas.microsoft.com/dynamics/2008/01/documents/Run"
exclude-result-prefixes="m r">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- move all elements to no namespace -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- rename MessageParts to Document + skip the Run wrapper -->
<xsl:template match="m:MessageParts">
<Document>
<xsl:apply-templates select="r:Run/*"/>
</Document>
</xsl:template>
<!-- rename RunObject to Item + reorder child nodes -->
<xsl:template match="r:RunObject[#class='entity']">
<Item>
<xsl:apply-templates select="r:A1" />
<xsl:apply-templates select="r:A2" />
<xsl:template match="r:Object[#class='entity']>
<Base>
<xsl:apply-templates select="r:A3" />
</Base>
</xsl:Template>
<xsl:apply-templates select="r:A4" />
</Item>
</xsl:template>
</xsl:stylesheet>
I tried matching the Object element but was unable to since i am already matching its parent element that is RunObject
Your mistake is that you cannot define a template inside of a template. So move the <xsl:template match="r:Object[#class='entity']> to the root level and add an <xsl:apply-templates select="r:Object" /> at its place.
The stylesheet could look like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" xmlns:r="http://schemas.microsoft.com/dynamics/2008/01/documents/Run" exclude-result-prefixes="m r">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- move all elements to no namespace -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- rename MessageParts to Document + skip the Run wrapper -->
<xsl:template match="m:MessageParts">
<Document>
<xsl:apply-templates select="r:Run/*"/>
</Document>
</xsl:template>
<!-- rename RunObject to Item + reorder child nodes -->
<xsl:template match="r:RunObject[#class='entity']">
<Item>
<xsl:apply-templates select="r:A1" />
<xsl:apply-templates select="r:A2" />
<xsl:apply-templates select="r:Object" />
<xsl:apply-templates select="r:A4" />
</Item>
</xsl:template>
<xsl:template match="r:Object[#class='entity']">
<Base>
<xsl:apply-templates select="r:A3" />
</Base>
</xsl:template>
</xsl:stylesheet>
Its output is (nearly) as desired:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<MessageId>{A124-B421-C325-D467}</MessageId>
<Action>find</Action>
</Header>
<Body>
<Document>
<Item>
<A1>NA</A1>
<A2>False</A2>
<Base>
<A3>02</A3>
</Base>
<A4>ER</A4>
</Item>
</Document>
</Body>
</Envelope>
I added the adjective "nearly", because this result has no namespace, while the sample of your desired outcome is in the namespace
xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
But because you defined a template that removes the namespaces of all elements, I did ignore that. I simply assume that this part of your question is erroneous.
If you like to change that, restrict the first template and add a general identity template:
<!-- move all elements to no namespace -->
<xsl:template match="*[namespace-uri() != 'http://schemas.microsoft.com/dynamics/2011/01/documents/Message']">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
This would make your output containing the root namespace:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>{A124-B421-C325-D467}</MessageId>
<Action>find</Action>
</Header>
<Body>
<Document xmlns="">
<Item>
<A1>NA</A1>
<A2>False</A2>
<Base>
<A3>02</A3>
</Base>
<A4>ER</A4>
</Item>
</Document>
</Body>
</Envelope>

Renaming the Parent node and Rearranging the Child nodes simultaneously in xml using xslt

How to rename the parent node and rearrange the child nodes simultaneously.
I tried some code but was unable to get the desired output.
This was the output that i was getting
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header> </Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Run xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Run">
<RunObject class="entity">
<A1>NA</A1>
<A2>False</A2>
<A3>02</A3>
<A4>ER</A4>
</RunObject>
<RunObject class="entity">
<A1>NA</A1>
<A2>False</A2>
<A3>03</A3>
<A4>ER</A4>
</RunObject>
</Run>
</MessageParts>
</Body>
</Envelope>
I wanted to remove the namespaces, rename the Parent tags and rearrange the child tags.
So i used this code
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:r="http://schemas.microsoft.com/dynamics/2008/01/documents/Run"
exclude-result-prefixes="m r">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- move all elements to no namespace -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- rename MessageParts to Document + skip the Run wrapper -->
<xsl:template match="m:MessageParts">
<Document>
<xsl:apply-templates select="r:Run/*"/>
</Document>
</xsl:template>
<xsl:template match="r:RunObject[#class='entity']">
<xsl:copy>
<xsl:apply-templates select="A3" />
<xsl:apply-templates select="A4" />
<xsl:apply-templates select="A2" />
<xsl:apply-templates select="A1" />
</xsl:copy>
</xsl:template>
<!-- rename RunObject to Item -->
<xsl:template match="r:RunObject[#class='entity']">
<Item>
<xsl:apply-templates />
</Item>
</xsl:template>
</xsl:stylesheet>
I was able to remove the namespaces, rename the parent tags but was unable to rearrange the child tags. The output i got was this
?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header> </Header>
<Body>
<Document>
<Item>
<A1>NA</A1>
<A2>False</A2>
<A3>02</A3>
<A4>ER</A4>
</Item>
<Item>
<A1>NA</A1>
<A2>False</A2>
<A3>03</A3>
<A4>ER</A4>
</Item>
</Document>
</Body>
</Envelope>
But the desired output is this
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header> </Header>
<Body>
<Document>
<Item>
<A3>02</A3>
<A4>ER</A4>
<A2>False</A2>
<A1>NA</A1>
</Item>
<Item>
<A3>03</A3>
<A4>ER</A4>
<A2>False</A2>
<A1>NA</A1>
</Item>
</Document>
</Body>
</Envelope>
Problem #1:
You have two templates matching the same nodes:
<xsl:template match="r:RunObject[#class='entity']">
Only the last one of these will be applied.
Problem #2:
The elements A1, A2, A3 and A4 are in a namespace inherited from their Run ancestor. You need to use the prefix bound to that namespace when selecting them.
See if this works for you:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:r="http://schemas.microsoft.com/dynamics/2008/01/documents/Run"
exclude-result-prefixes="m r">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- move all elements to no namespace -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- rename MessageParts to Document + skip the Run wrapper -->
<xsl:template match="m:MessageParts">
<Document>
<xsl:apply-templates select="r:Run/*"/>
</Document>
</xsl:template>
<!-- rename RunObject to Item + reorder child nodes -->
<xsl:template match="r:RunObject[#class='entity']">
<Item>
<xsl:apply-templates select="r:A3" />
<xsl:apply-templates select="r:A4" />
<xsl:apply-templates select="r:A2" />
<xsl:apply-templates select="r:A1" />
</Item>
</xsl:template>
</xsl:stylesheet>

select every following segment until next sibling occurs and group them under target structure

I have input structure like below:
After Item we have delivery_Info segment it can one per item or multiple for item, And the again Item followed by Delivery_Info. So we have group first item with following Delivery_Info and then next Item with their following Delivery_Info. Input and output structure is provided for more understanding.
DataSet cardinality is 1..n
EDI_Header cardinality is 1..n
Item cardinality is 1..n
Deliveryinfoset cardinality 1..1
Delivery_info cardinality 0..n
<?xml version="1.0" encoding="utf-8"?>
<ns:MM_FlatFile_CBOX_ORDRSP_Out xmlns:ns="urn:test:corp:pi:a:ecc:cbox:ordrsp:100">
<DataSet>
<EDI_Header>
<LineIdent>SIM</LineIdent>
<EDIPartnerNo>047866091</EDIPartnerNo>
<MessageType>ORDRSP01</MessageType>
<VersionNo>3A7V0</VersionNo>
<Mode>102</Mode>
</EDI_Header>
<Header>
<LineIdent>HO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<CustomerOrderNo>0038283143</CustomerOrderNo>
<MessageFunction/>
<Reference1>6174625</Reference1>
<Reference2/>
<Reference3/>
<SoldToID>126919</SoldToID>
<Partner2>126919</Partner2>
<Partner3/>
<ShipToID>126919</ShipToID>
<ShipToName>XXXXX</ShipToName>
<ShipToAddress/>
<ShipToAddress2>XXXXX</ShipToAddress2>
<ShipToCity>XXXXX</ShipToCity>
<ShipToZIP>XXXXX</ShipToZIP>
<ShipToCountry>FI</ShipToCountry>
<CustContact>XXXXX</CustContact>
<CustContactTelNo>XXXXX</CustContactTelNo>
<TransportType>EUR</TransportType>
</Header>
<Item>
<LineIdent>PO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1</LineItemNo>
<ActionCode>6</ActionCode>
<ItemNoCust>P380200</ItemNoCust>
<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
<Quantity>D1L1</Quantity>
<Unit>PCE</Unit>
<Price>0.4000</Price>
<PriceType/>
<QuantityPriceUnit>1</QuantityPriceUnit>
<LineItemNoCust/>
</Item>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
<Item>
<LineIdent>PO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1</LineItemNo>
<ActionCode>6</ActionCode>
<ItemNoCust>P380200</ItemNoCust>
<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
<Quantity>D1L1</Quantity>
<Unit>PCE</Unit>
<Price>0.4000</Price>
<PriceType/>
<QuantityPriceUnit>1</QuantityPriceUnit>
<LineItemNoCust/>
</Item>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
</DataSet>
<DataSet>
<EDI_Header>
<LineIdent>SIM</LineIdent>
<EDIPartnerNo>047866091</EDIPartnerNo>
<MessageType>ORDRSP01</MessageType>
<VersionNo>3A7V0</VersionNo>
<Mode>102</Mode>
</EDI_Header>
<Header>
<LineIdent>HO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<CustomerOrderNo>0038283143</CustomerOrderNo>
<MessageFunction/>
<Reference1>6174625</Reference1>
<Reference2/>
<Reference3/>
<SoldToID>126919</SoldToID>
<Partner2>126919</Partner2>
<Partner3/>
<ShipToID>126919</ShipToID>
<ShipToName>XXXXX</ShipToName>
<ShipToAddress/>
<ShipToAddress2>XXXXX</ShipToAddress2>
<ShipToCity>XXXXX</ShipToCity>
<ShipToZIP>XXXXX</ShipToZIP>
<ShipToCountry>FI</ShipToCountry>
<CustContact>XXXXX</CustContact>
<CustContactTelNo>XXXXX</CustContactTelNo>
<TransportType>EUR</TransportType>
</Header>
<Item>
<LineIdent>PO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1</LineItemNo>
<ActionCode>6</ActionCode>
<ItemNoCust>P380200</ItemNoCust>
<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
<Quantity>D1L1</Quantity>
<Unit>PCE</Unit>
<Price>0.4000</Price>
<PriceType/>
<QuantityPriceUnit>1</QuantityPriceUnit>
<LineItemNoCust/>
</Item>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
</DataSet>
</ns:MM_FlatFile_CBOX_ORDRSP_Out>
Target Structure should be-->
<?xml version="1.0" encoding="utf-8"?>
<ns:MM_FlatFile_CBOX_ORDRSP_Out
xmlns:ns="urn:test:corp:pi:a:ecc:cbox:ordrsp:100">
<DataSet>
<EDI_Header>
<LineIdent>SIM</LineIdent>
<EDIPartnerNo>047866091</EDIPartnerNo>
<MessageType>ORDRSP01</MessageType>
<VersionNo>3A7V0</VersionNo>
<Mode>102</Mode>
</EDI_Header>
<Header>
<LineIdent>HO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<CustomerOrderNo>0038283143</CustomerOrderNo>
<MessageFunction/>
<Reference1>6174625</Reference1>
<Reference2/>
<Reference3/>
<SoldToID>126919</SoldToID>
<Partner2>126919</Partner2>
<Partner3/>
<ShipToID>126919</ShipToID>
<ShipToName>XXXXX</ShipToName>
<ShipToAddress/>
<ShipToAddress2>XXXXX</ShipToAddress2>
<ShipToCity>XXXXX</ShipToCity>
<ShipToZIP>XXXXX</ShipToZIP>
<ShipToCountry>FI</ShipToCountry>
<CustContact>XXXXX</CustContact>
<CustContactTelNo>XXXXX</CustContactTelNo>
<TransportType>EUR</TransportType>
</Header>
<Item>
<LineIdent>PO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1</LineItemNo>
<ActionCode>6</ActionCode>
<ItemNoCust>P380200</ItemNoCust>
<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
<Quantity>D1L1</Quantity>
<Unit>PCE</Unit>
<Price>0.4000</Price>
<PriceType/>
<QuantityPriceUnit>1</QuantityPriceUnit>
<LineItemNoCust/>
<Deliveryinfoset>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
</Deliveryinfoset>
</Item>
<Item>
<LineIdent>PO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1</LineItemNo>
<ActionCode>6</ActionCode>
<ItemNoCust>P380200</ItemNoCust>
<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
<Quantity>D1L1</Quantity>
<Unit>PCE</Unit>
<Price>0.4000</Price>
<PriceType/>
<QuantityPriceUnit>1</QuantityPriceUnit>
<LineItemNoCust/>
<Deliveryinfoset>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
</Deliveryinfoset>
</Item>
</DataSet>
<DataSet>
<EDI_Header>
<LineIdent>SIM</LineIdent>
<EDIPartnerNo>047866091</EDIPartnerNo>
<MessageType>ORDRSP01</MessageType>
<VersionNo>3A7V0</VersionNo>
<Mode>102</Mode>
</EDI_Header>
<Header>
<LineIdent>HO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<CustomerOrderNo>0038283143</CustomerOrderNo>
<MessageFunction/>
<Reference1>6174625</Reference1>
<Reference2/>
<Reference3/>
<SoldToID>126919</SoldToID>
<Partner2>126919</Partner2>
<Partner3/>
<ShipToID>126919</ShipToID>
<ShipToName>XXXXX</ShipToName>
<ShipToAddress/>
<ShipToAddress2>XXXXX</ShipToAddress2>
<ShipToCity>XXXXX</ShipToCity>
<ShipToZIP>XXXXX</ShipToZIP>
<ShipToCountry>FI</ShipToCountry>
<CustContact>XXXXX</CustContact>
<CustContactTelNo>XXXXX</CustContactTelNo>
<TransportType>EUR</TransportType>
</Header>
<Item>
<LineIdent>PO01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1</LineItemNo>
<ActionCode>6</ActionCode>
<ItemNoCust>P380200</ItemNoCust>
<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
<Quantity>D1L1</Quantity>
<Unit>PCE</Unit>
<Price>0.4000</Price>
<PriceType/>
<QuantityPriceUnit>1</QuantityPriceUnit>
<LineItemNoCust/>
<Deliveryinfoset>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
<Delivery_Info>
<LineIdent>TE01</LineIdent>
<SupplierOrderNo>6174625</SupplierOrderNo>
<LineItemNo>D1L1DI1</LineItemNo>
<Quantity>3000</Quantity>
<Unit>PCE</Unit>
<DelDateReq>20181130</DelDateReq>
<DelDate>20181212</DelDate>
<DateCode>C</DateCode>
<QuantityOrdered>3000</QuantityOrdered>
<DateCurrent>20181121</DateCurrent>
<SysVar/>
</Delivery_Info>
</Deliveryinfoset>
</Item>
</DataSet>
</ns:MM_FlatFile_CBOX_ORDRSP_Out>
My XSLT Code which is not working if we have more than one item with their delivery info
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ns="urn:test:corp:pi:a:ecc:cbox:ordrsp:100" exclude-result-prefixes="ns">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<MM_FlatFile_CBOX_ORDRSP_Out>
<xsl:for-each select="/ns:MM_FlatFile_CBOX_ORDRSP_Out/DataSet">
<DataSet>
<EDI_Header>
<LineIdent>
<xsl:value-of select="./EDI_Header/LineIdent"/>
</LineIdent>
<EDIPartnerNo>
<xsl:value-of select="./EDI_Header/EDIPartnerNo"/>
</EDIPartnerNo>
<MessageType>
<xsl:value-of select="'ORDRSP01'"/>
</MessageType>
<VersionNo>
<xsl:value-of select="'3A7V0'"/>
</VersionNo>
<Mode>
<xsl:value-of select="'102'"/>
</Mode>
</EDI_Header>
<Header>
<LineIdent>
<xsl:value-of select="./Header/LineIdent"/>
</LineIdent>
<SupplierOrderNo>
<xsl:value-of select="./Header/SupplierOrderNo"/>
</SupplierOrderNo>
<CustomerOrderNo>
<xsl:value-of select="./Header/CustomerOrderNo"/>
</CustomerOrderNo>
<MessageFunction>
<xsl:value-of select="./Header/MessageFunction"/>
</MessageFunction>
<Reference1>
<xsl:value-of select="./Header/Reference1"/>
</Reference1>
<Reference2>
<xsl:value-of select="./Header/Reference2"/>
</Reference2>
<Reference3>
<xsl:value-of select="./Header/Reference3"/>
</Reference3>
<SoldToID>
<xsl:value-of select="./Header/SoldToID"/>
</SoldToID>
<Partner2>
<xsl:value-of select="./Header/Partner2"/>
</Partner2>
<Partner3>
<xsl:value-of select="./Header/Partner3"/>
</Partner3>
<ShipToID>
<xsl:value-of select="./Header/ShipToID"/>
</ShipToID>
<ShipToName>
<xsl:value-of select="./Header/ShipToName"/>
</ShipToName>
<ShipToAddress>
<xsl:value-of select="./Header/ShipToAddress"/>
</ShipToAddress>
<ShipToAddress2>
<xsl:value-of select="./Header/ShipToAddress2"/>
</ShipToAddress2>
<ShipToCity>
<xsl:value-of select="./Header/ShipToCity"/>
</ShipToCity>
<ShipToZIP>
<xsl:value-of select="./Header/ShipToZIP"/>
</ShipToZIP>
<ShipToCountry>
<xsl:value-of select="./Header/ShipToCountry"/>
</ShipToCountry>
<CustContact>
<xsl:value-of select="./Header/CustContact"/>
</CustContact>
<CustContactTelNo>
<xsl:value-of select="./Header/CustContactTelNo"/>
</CustContactTelNo>
<TransportType>
<xsl:value-of select="./Header/TransportType"/>
</TransportType>
</Header>
<xsl:for-each select="./Item">
<Item>
<LineIdent>
<xsl:value-of select="./LineIdent"/>
</LineIdent>
<SupplierOrderNo>
<xsl:value-of select="./SupplierOrderNo"/>
</SupplierOrderNo>
<LineItemNo>
<xsl:value-of select="./LineItemNo"/>
</LineItemNo>
<ActionCode>
<xsl:value-of select="./ActionCode"/>
</ActionCode>
<ItemNoCust>
<xsl:value-of select="./ItemNoCust"/>
</ItemNoCust>
<ItemNoSup>
<xsl:value-of select="./ItemNoSup"/>
</ItemNoSup>
<Quantity>
<xsl:value-of select="./Quantity"/>
</Quantity>
<Unit>
<xsl:value-of select="./Unit"/>
</Unit>
<Price>
<xsl:value-of select="./Price"/>
</Price>
<PriceType>
<xsl:value-of select="./PriceType"/>
</PriceType>
<QuantityPriceUnit>
<xsl:value-of select="./QuantityPriceUnit"/>
</QuantityPriceUnit>
<LineItemNoCust>
<xsl:value-of select="./LineItemNoCust"/>
</LineItemNoCust>
<xsl:for-each select="../Delivery_Info">
<DeliveryInfoSet>
<Delivery_Info>
<LineIdent>
<xsl:value-of select="./LineIdent"/>
</LineIdent>
<SupplierOrderNo>
<xsl:value-of select="./SupplierOrderNo"/>
</SupplierOrderNo>
<LineItemNo>
<xsl:value-of select="./LineItemNo"/>
</LineItemNo>
<Quantity>
<xsl:value-of select="./Quantity"/>
</Quantity>
<Unit>
<xsl:value-of select="./Unit"/>
</Unit>
<DelDateReq>
<xsl:value-of select="./DelDateReq"/>
</DelDateReq>
<DelDate>
<xsl:value-of select="./DelDate"/>
</DelDate>
<DateCode>
<xsl:value-of select="./DateCode"/>
</DateCode>
<QuantityOrdered>
<xsl:value-of select="./QuantityOrdered"/>
</QuantityOrdered>
<DateCurrent>
<xsl:value-of select="./DateCurrent"/>
</DateCurrent>
<SysVar>
<xsl:value-of select="./SysVar"/>
</SysVar>
</Delivery_Info>
</DeliveryInfoSet>
</xsl:for-each>
</Item>
</xsl:for-each>
</DataSet>
</xsl:for-each>
</MM_FlatFile_CBOX_ORDRSP_Out>
</xsl:template>
</xsl:stylesheet>
Thanks
May I request you people to help me in this requirement
Given a well-formed (!) input such as:
XML
<Root>
<Header>
<Header_No>XXX</Header_No>
</Header>
<Item>
<Item_No>1</Item_No>
<Price>XXX</Price>
</Item>
<Delivery_Info>
<Delivery_no>101</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Delivery_Info>
<Delivery_no>102</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Delivery_Info>
<Delivery_no>103</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Item>
<Item_No>2</Item_No>
<Price>XXX</Price>
</Item>
<Delivery_Info>
<Delivery_no>201</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Delivery_Info>
<Delivery_no>202</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
</Root>
the following stylesheet:
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:strip-space elements="*"/>
<xsl:key name="delivery" match="Delivery_Info" use="generate-id(preceding-sibling::Item[1])" />
<xsl:template match="/Root">
<xsl:copy>
<xsl:copy-of select="Header"/>
<xsl:for-each select="Item">
<xsl:copy>
<xsl:copy-of select="*"/>
<Deliveryinfoset>
<xsl:copy-of select="key('delivery', generate-id())"/>
</Deliveryinfoset>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Header>
<Header_No>XXX</Header_No>
</Header>
<Item>
<Item_No>1</Item_No>
<Price>XXX</Price>
<Deliveryinfoset>
<Delivery_Info>
<Delivery_no>101</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Delivery_Info>
<Delivery_no>102</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Delivery_Info>
<Delivery_no>103</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
</Deliveryinfoset>
</Item>
<Item>
<Item_No>2</Item_No>
<Price>XXX</Price>
<Deliveryinfoset>
<Delivery_Info>
<Delivery_no>201</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
<Delivery_Info>
<Delivery_no>202</Delivery_no>
<Delivery_Date>XXXXX</Delivery_Date>
</Delivery_Info>
</Deliveryinfoset>
</Item>
</Root>

XSL Sorting Numbers by position of nodes

I want to sort out the values of "number" by the Position/or first come in the XML document of the T.number values and display it. Is there a way to do this?
Here is my XML document
<?xml version="1.0" encoding="UTF-8"?>
<JobList>
<Job ID="2" />
<Job ID="3" />
<Job ID="5" />
<Job ID="4" />
<Job ID="6" />
<Job T.number="28" />
<Job T.number="10" />
<Job T.number="24" />
<Job T.number="75" />
<Job T.number="75" />
<Tool number="10" />
<Tool number="24" />
<Tool number="28" />
<Tool number="75" />
<Tool number="75" />
</JobList>
Desired Result:
<?xml version="1.0" encoding="UTF-8"?>
<JobList>
<Job ID="2" />
<Job ID="3" />
<Job ID="5" />
<Job ID="4" />
<Job ID="6" />
<Tool number="28" />
<Tool number="10" />
<Tool number="24" />
<Tool number="75" />
</JobList>
Here is the XSL document:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" encoding="UTF-8" method="xml" />
<xsl:param name="REPORT">joblist</xsl:param>
<xsl:param name="LOCALE">en-US</xsl:param>
<xsl:param name="FORMAT">html</xsl:param>
<xsl:param name="CAPTURE">example,job</xsl:param>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Joblist Report</title>
<style type="text/css">
body {font-family: Arial;}
</style>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="JobList">
<div>
<table width="100" border="1">
<thead>
<tr>
<td>
<xsl:value-of select="Sorted Numbers" />
</td>
</tr>
</thead>
<tbody>
<xsl:variable name="vsortOrder" select="//Job[#T.number]" />
<xsl:for-each select="Tool">
<xsl:sort select="#number" order="{$vsortOrder}" data-type="number" />
<tr>
<td>
<xsl:value-of select="#number" />
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</xsl:template>
</xsl:stylesheet>
UPDATE: I have found an attribute that has the same number order I need. I updated the XML with the attribute. But the XSL still doesnt want to sort it out.
Your desired output looks a lot different than what your actual code would produce. But maybe this will help you. This code will get your desired output. Then you can modify it to be what you actually want.
<!-- Change your for-each loop. -->
<xsl:for-each select="$vsortOrder/#T.number">
<xsl:variable name="tnumber" select="."/>
<xsl:copy-of select="//Tool[#number=$tnumber]"/>
</xsl:for-each>
Or, you could modify your code to do this. Since you are only printing out the number, you do not have to lookup the tool.
<!-- Change your for-each loop. -->
<xsl:for-each select="$vsortOrder/#T.number">
<tr>
<td>
<xsl:value-of select="." />
</td>
</tr>
</xsl:for-each>