How to set Line item number for each group - xslt-1.0

Can someone please help me out with this scenario asap?
Input XML:
<dataXML>
<Items>
<itemLine>
<lineNo>1</lineNo>
<lineRefNo>001</lineRefNo>
<lineDes>test1</lineDes>
</itemLine>
<itemLine>
<lineNo>2</lineNo>
<lineRefNo>001</lineRefNo>
<lineDes>test2</lineDes>
</itemLine>
<itemLine>
<lineNo>3</lineNo>
<lineRefNo>002</lineRefNo>
<lineDes>test3</lineDes>
</itemLine>
<itemLine>
<lineNo>4</lineNo>
<lineRefNo>002</lineRefNo>
<lineDes>test4</lineDes>
</itemLine>
<itemLine>
<lineNo>5</lineNo>
<lineRefNo>002</lineRefNo>
<lineDes>test5</lineDes>
</itemLine>
<itemLine>
<lineNo>6</lineNo>
<lineRefNo>003</lineRefNo>
<lineDes>test6</lineDes>
</itemLine>
</Items>
I have regrouped item lines based on LineRefNo
Transformation xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="totalLine" select="count(dataXML/Items/itemLine)" />
<Root>
<xsl:value-of select="concat('<','test','>')" disable-output-escaping="yes"/>
<xsl:for-each select="dataXML/Items/itemLine">
<xsl:choose>
<xsl:when test="lineRefNo != preceding-sibling::itemLine[1]/lineRefNo">
<xsl:value-of select="concat('</','test','>')" disable-output-escaping="yes"/>
<xsl:value-of select="concat('<','test','>')" disable-output-escaping="yes"/>
<item>
<xsl:value-of select="concat('Line No : ',position())" />,<xsl:value-of select="lineRefNo" />
</item>
<xsl:if test="$totalLine = position()">
<xsl:value-of select="concat('</','test','>')" disable-output-escaping="yes"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="concat('Line No : ',position())" />,<xsl:value-of select="lineRefNo" />
</item>
<xsl:if test="$totalLine = position()">
<xsl:value-of select="concat('</','test','>')" disable-output-escaping="yes"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</Root>
</xsl:template>
</xsl:stylesheet>
Note : Please escape '>' and '<' in xslt transformation where Test and Item tags are used
It will transform input xml as below result
**Result :**
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<test>
<item>Line No : 1,001</item>
<item>Line No : 2,001</item>
</test>
<test>
<item>Line No : 3,002</item>
<item>Line No : 4,002</item>
<item>Line No : 5,002</item>
</test>
<test>
<item>Line No : 6,003</item>
</test>
</Root>
**Expected Result:**
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<test>
<item>Line No : 1,001</item>
<item>Line No : 2,001</item>
</test>
<test>
<item>Line No : 1,002</item>
<item>Line No : 2,002</item>
<item>Line No : 3,002</item>
</test>
<test>
<item>Line No : 1,003</item>
</test>
</Root>
Thanking in Advance

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:key name="kItemsByRef" match="itemLine"
use="lineRefNo"/>
<xsl:template match=
"itemLine
[generate-id()
=
generate-id(key('kItemsByRef', lineRefNo)[1])
]
">
<test>
<xsl:for-each select="key('kItemsByRef', lineRefNo)">
<item>Line No : <xsl:value-of select=
"concat(position(), ',', lineRefNo)"/>
</item>
</xsl:for-each>
</test>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
when applied on the provided XML document:
<dataXML>
<Items>
<itemLine>
<lineNo>1</lineNo>
<lineRefNo>001</lineRefNo>
<lineDes>test1</lineDes>
</itemLine>
<itemLine>
<lineNo>2</lineNo>
<lineRefNo>001</lineRefNo>
<lineDes>test2</lineDes>
</itemLine>
<itemLine>
<lineNo>3</lineNo>
<lineRefNo>002</lineRefNo>
<lineDes>test3</lineDes>
</itemLine>
<itemLine>
<lineNo>4</lineNo>
<lineRefNo>002</lineRefNo>
<lineDes>test4</lineDes>
</itemLine>
<itemLine>
<lineNo>5</lineNo>
<lineRefNo>002</lineRefNo>
<lineDes>test5</lineDes>
</itemLine>
<itemLine>
<lineNo>6</lineNo>
<lineRefNo>003</lineRefNo>
<lineDes>test6</lineDes>
</itemLine>
</Items>
</dataXML>
produces the wanted, correct result:
<test>
<item>Line No : 1,001</item>
<item>Line No : 2,001</item>
</test>
<test>
<item>Line No : 1,002</item>
<item>Line No : 2,002</item>
<item>Line No : 3,002</item>
</test>
<test>
<item>Line No : 1,003</item>
</test>

Related

XSLT 1.0 grouping on one or multiple levels

Today's challenge was grouping in XSLT 1.0. Found out there are something called keys and the Muenchian grouping.
Input XML:
<Items>
<Item>
<ID>1</ID>
<Name>A</Name>
<Country>Sweden</Country>
<Region>Småland</Region>
</Item>
<Item>
<ID>2</ID>
<Name>B</Name>
<Country>Sweden</Country>
<Region>Norrland</Region>
</Item>
<Item>
<ID>3</ID>
<Name>C</Name>
<Country>USA</Country>
<Region>Alaska</Region>
</Item>
<Item>
<ID>4</ID>
<Name>D</Name>
<Country>USA</Country>
<Region>Texas</Region>
</Item>
<Item>
<ID>5</ID>
<Name>E</Name>
<Country>Sweden</Country>
<Region>Norrland</Region>
</Item>
</Items>
I need to make thins XML into a better structure, and from this sample XML I't like to get items structured by country and region. Below is wanted result where country and region gets sorted as well:
<Items>
<Country Name="Sweden">
<Region Name="Norrland">
<Item>
<ID>2</ID>
<Name>B</Name>
</Item>
<Item>
<ID>5</ID>
<Name>E</Name>
</Item>
</Region>
<Region Name="Småland">
<Item>
<ID>1</ID>
<Name>A</Name>
</Item>
</Region>
</Country>
<Country Name="USA">
<Region Name="Alaska">
<Item>
<ID>3</ID>
<Name>C</Name>
</Item>
</Region>
<Region Name="Texas">
<Item>
<ID>4</ID>
<Name>D</Name>
</Item>
</Region>
</Country>
</Items>
EDIT:
I also want to make sure regions end up in their own country, even if there are duplicates. I edited the answer accordingly.
Also, I'd like to hint about xsltfiddle.liberty-development.net as an easy way of doing trial-and-error XSLT development...
Inspired by this article, I found a neat solution to this problem:
I have included comments for using it for single or double grouping, see comments in the code. Notice how I use first key (index) as input to the secon for-each loop:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="country" match="Item" use="Country" />
<xsl:key name="region" match="Item" use="concat(Region, '|', Country)" />
<xsl:template match="/Items">
<Items>
<xsl:for-each select="Item[generate-id(.) = generate-id(key('country', Country))]">
<xsl:sort select="Country" />
<xsl:variable name="_country" select="Country" />
<xsl:element name="Country">
<xsl:attribute name="Name"><xsl:value-of select="$_country" /></xsl:attribute>
<!-- single level grouping -->
<!--<xsl:apply-templates select="key('country', Country)" />-->
<!-- double grouping -->
<!-- START -->
<xsl:for-each select="key('country', Country)[generate-id(.) = generate-id(key('region', concat(Region, '|', Country)))]">
<xsl:sort select="Region" />
<xsl:variable name="_region" select="Region" />
<xsl:element name="Region">
<xsl:attribute name="Name"><xsl:value-of select="$_region" /></xsl:attribute>
<xsl:apply-templates select="key('region', concat(Region, '|', Country))" />
</xsl:element>
</xsl:for-each>
<!-- END -->
</xsl:element>
</xsl:for-each>
</Items>
</xsl:template>
<xsl:template match="Item">
<xsl:element name="Item">
<xsl:element name="ID"><xsl:value-of select="ID" /></xsl:element>
<xsl:element name="Name"><xsl:value-of select="Name" /></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

xsl key match to return specific value

input:
<DS>
<TL>
<msg>
<output_getquerydata>
<queries>
<query name="q1">
<parameters>
<parameter name="id">906OREA</parameter>
</parameters>
<results>
<record>
<column name="actionState">sdss</column>
</record>
</results>
</query>
<query name="q2">
<parameters>
<parameter name="resCode">CTL</parameter>
<parameter name="prodCode">89CMID</parameter>
<parameter name="pos">1,2,4,3</parameter>
</parameters>
<results>
<record id="1">
<column name="position">1</column>
<column name="ExternalProductId"/>
</record>
<record id="9">
<column name="position"/>
<column name="ExternalProductId">316442</column>
</record>
</results>
</query>
<query name="q2">
<parameters>
<parameter name="resCode">CTL</parameter>
<parameter name="prodCode">91VPRM</parameter>
<parameter name="pos">1,2,4,3</parameter>
</parameters>
<results>
<record id="1">
<column name="position"/>
<column name="ExternalProductId">316495</column>
</record>
</results>
</query>
</queries>
</output_getquerydata>
</msg>
<TL>
<ArticleNr>89CMID</ArticleNr>
</TL>
<TL>
<ArticleNr>89CMID</ArticleNr>
</TL>
<TL>
<ArticleNr>89CMID</ArticleNr>
</TL>
<TL>
<ArticleNr>91VPRM</ArticleNr>
</TL>
</TL>
</DS>
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Article" match="tLoading" use="./ArticleNr"/>
<xsl:key name="prod" match="query[#name='q2']/results/record" use="./column[#name='ExternalProductId'][node()]"/>
<xsl:template match="DS">
<msglist>
<xsl:for-each select="./TL[./msg/output_getquerydata/queries/query/results/record/column[#name='actionState'] !='finished'] ">
<xsl:variable name="distinctArticle" select="//TL[string(ArticleNr)][count(. | key('Article',ArticleNr)[1]) = 1]"/>
<msg>
<xsl:for-each select="$distinctArticle">
<load-part>
<!--I need here the value from column[#name='ExtPR'], that has parameter[#name='prodCode']=the current TL articleNr node.
-->
<productId>
<xsl:value-of select="key('prod',column[#name='ExternalProductId'])"/>
</productId>
<!--something-->
</load-part>
</xsl:for-each>
</msg>
</xsl:for-each>
</msglist>
</xsl:template>
</xsl:stylesheet>
Desired OUTPUT:
<msglist>
<msg>
<load-part>
<productId>316442</productId>
</load-part>
<load-part>
<productId>316442</productId>
</load-part>
<load-part>
<productId>316442</productId>
</load-part>
<load-part>
<productId>316495</productId>
</load-part>
</msg>
</msglist>
I need in the productID node, the value from column[#name='ExternalProductId'], that has parameter[#name='prodCode']=the current <TL><ArticleNr> node.
I know that this 'for each' code that I've put, returns only two values, because i'm searching for the distinct values, so I was thinking that I will try with a key, but i'm not sure what I am missing.
Thank you
edited for the correct output values
Firstly, your Article key looks wrong (in the context of your question) as there are no tLoading elements in your XML. So it should be this...
<xsl:key name="Article" match="TL" use="ArticleNr"/>
But to answer your direct question, you need to define your prod key like so
<xsl:key name="prod"
match="query[#name='q2']/results/record"
use="../../parameters/parameter[#name='prodCode']"/>
Then, to look it up, do this...
<xsl:value-of select="key('prod', ArticleNr)/column[#name='ExternalProductId']"/>
Or maybe this, as they "q2" query has two ExternalProductIds...
<xsl:value-of select="key('prod', ArticleNr)/column[#name='ExternalProductId'][. != '']"/>
Try this XSLT (which retains your distinct check, and so only outputs two rows)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="Article" match="TL" use="ArticleNr"/>
<xsl:key name="prod" match="query[#name='q2']/results/record" use="../../parameters/parameter[#name='prodCode']"/>
<xsl:template match="DS">
<msglist>
<xsl:for-each select="./TL[./msg/output_getquerydata/queries/query/results/record/column[#name='actionState'] !='finished'] ">
<xsl:variable name="distinctArticle" select="//TL[string(ArticleNr)][count(. | key('Article',ArticleNr)[1]) = 1]"/>
<msg>
<xsl:for-each select="$distinctArticle">
<load-part>
<productId>
<xsl:value-of select="ArticleNr" />
<xsl:text> - </xsl:text>
<xsl:value-of select="key('prod', ArticleNr)/column[#name='ExternalProductId'][. != '']"/>
</productId>
</load-part>
</xsl:for-each>
</msg>
</xsl:for-each>
</msglist>
</xsl:template>
</xsl:stylesheet>

XSLT: Convert Name/Value pair and transform an XML

I need to convert a name value pair into XML. I'm able to generate an XML, but the element name should be grouped and it should not be duplicated. Please see below. The FieldValue element contains 2 OrderItem values in the Detail node. If the FieldValue with OrderItem repeats, then the result should be grouped into one OrderItem node. Please help.
Source XML:
<SC>
<Header>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderHeader</FieldValue>
</Record>
<Record>
<FieldName>Order</FieldName>
<FieldValue>1234</FieldValue>
</Record>
</Header>
<Detail>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderItem</FieldValue>
</Record>
<Record>
<FieldName>Item</FieldName>
<FieldValue>1</FieldValue>
</Record>
<Record>
<FieldName>Qty</FieldName>
<FieldValue>10</FieldValue>
</Record>
</Detail>
<Detail>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderItem</FieldValue>
</Record>
<Record>
<FieldName>Item</FieldName>
<FieldValue>2</FieldValue>
</Record>
<Record>
<FieldName>Qty</FieldName>
<FieldValue>20</FieldValue>
</Record>
</Detail>
</SC>
Target XML:
<Order>
<OrderItem>
<Item>
<Item>1</Item>
<Qty>10</Qty>
</Item>
<Item>
<Item>2</Item>
<Qty>20</Qty>
</Item>
</OrderItem>
</Order>
XSLT:
<xsl:template match="#*|node()">
<Order>
<xsl:for-each select="Detail">
<Item>
<xsl:apply-templates select="Record[position()>1]"/>
</Item>
</xsl:for-each>
</Order>
</xsl:template>
<xsl:template match="Record">
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>
The grouping can be done as follows:
<?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 indent="yes"/>
<xsl:template match="SC">
<Order>
<xsl:for-each-group select="Detail" group-by="Record[1]/FieldValue">
<xsl:element name="{current-grouping-key()}">
<xsl:apply-templates select="current-group()"/>
</xsl:element>
</xsl:for-each-group>
</Order>
</xsl:template>
<xsl:template match="Detail">
<Item>
<xsl:apply-templates select="Record[position() gt 1]"/>
</Item>
</xsl:template>
<xsl:template match="Record">
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It appears that you are trying to define two XSLT templates, when one should be sufficient. You want to match on the root and then that you want to iterate over each SC/Detail.
Then, you want to take the FieldValue of the sibling of the FieldName node that is 'Item' (for item value) and 'Qty' (for quantity value), but only those listed under 'Record'.
Note: You have specified a doubly-nested <Item> in your transformed output and this solution reflects that requirement.
This XSLT should do what you are requesting:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="SC/Detail">
<Order>
<OrderItem>
<Item>
<Item>
<xsl:value-of select="Record[FieldName[text()='Item']]/FieldValue" />
</Item>
<Qty>
<xsl:value-of select="Record[FieldName[text()='Qty']]/FieldValue" />
</Qty>
</Item>
</OrderItem>
</Order>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Based on the object corresponding values should be taken using xslt

INPUT XML
<root>
<file1>
<commodity>
<units>1</units>
<obj>mango</obj>
</commodity>
<commodity>
<units>5</units>
<obj>guava</obj>
</commodity>
</file1>
<file2>
<category>
<object>guava</object>
<type>CAT1</type>
<colour>green</colour>
</category>
<category>
<object>mango</object>
<type>CAT2</type>
<colour>yellow</colour>
</category>
</file2>
</root>
I need to compare the values of obj in file1 and object in file2 under root, if same I need to take their corresponding units, type and colour and produce the following output using xslt.
OUTPUT XML
<output>
<com>
<name>guava</name>
<num>5</num>
<category>CAT1</category>
<col>green</col>
</com>
<com>
<name>mango</name>
<num>1</num>
<category>CAT2</category>
<col>yellow</col>
</com>
</output>
I tried the below XSLT but the response is not as expected. Its not looping properly. Could you please tell me where I am going wrong.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes" />
<xsl:key name="object-search" match="root/file1/commodity" use="obj" />
<xsl:template match="/">
<output>
<xsl:for-each select="key('object-search', //category/object)">
<com>
<name>
<xsl:value-of select="obj" />
</name>
<num>
<xsl:value-of select="units" />
</num>
<category>
<xsl:value-of
select="//root/file2/category/type" />
</category>
<col>
<xsl:value-of
select="//root/file2/category/colour" />
</col>
</com>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
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:key name="cat" match="category" use="object" />
<xsl:template match="/root">
<output>
<xsl:for-each select="file1/commodity">
<com>
<name>
<xsl:value-of select="obj" />
</name>
<num>
<xsl:value-of select="units" />
</num>
<xsl:variable name="cat" select="key('cat', obj)" />
<category>
<xsl:value-of select="$cat/type" />
</category>
<col>
<xsl:value-of select="$cat/colour" />
</col>
</com>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Note that the result is slightly different from what you posted:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<com>
<name>mango</name>
<num>1</num>
<category>CAT2</category>
<col>yellow</col>
</com>
<com>
<name>guava</name>
<num>5</num>
<category>CAT1</category>
<col>green</col>
</com>
</output>
Alternatively, you could do:
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="com" match="commodity" use="obj" />
<xsl:template match="/root">
<output>
<xsl:for-each select="file2/category">
<xsl:variable name="com" select="key('com', object)" />
<com>
<name>
<xsl:value-of select="$com/obj" />
</name>
<num>
<xsl:value-of select="$com/units" />
</num>
<category>
<xsl:value-of select="type" />
</category>
<col>
<xsl:value-of select="colour" />
</col>
</com>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
and get:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<com>
<name>guava</name>
<num>5</num>
<category>CAT1</category>
<col>green</col>
</com>
<com>
<name>mango</name>
<num>1</num>
<category>CAT2</category>
<col>yellow</col>
</com>
</output>

XSLT get Node by Index

i am beginner in XSLT and i am using it to transform XML to XML
This is the source XML i receive
Source XML:
<Response>
<Pax>
<Id>1</Id>
</Pax>
<Pax>
<Id>2</Id>
</Pax>
<Travelers>
<Traveler>
<Name>ABC</Name>
</Traveler>
<Traveler>
<Name>XYZ</Name>
</Traveler>
</Travelers>
</Response>
I have written below XSLT
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="Response">
<xsl:element name="Root">
<xsl:apply-templates select="Travelers/Traveler"/>
</xsl:element>
</xsl:template>
<xsl:template match="Traveler">
<xsl:element name="Person">
<xsl:element name="PId">
<xsl:value-of select="//Pax/Id[position()]" />
</xsl:element>
<xsl:element name="Name">
<xsl:value-of select="Name" />
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<Root>
<Person>
<PId>1</PId>
<Name>ABC</Name>
</Person>
<Person>
<PId>1</PId>
<Name>XYZ</Name>
</Person>
</Root>
I would like to generate below XML output
Expected Output:
<Root>
<Person>
<PId>1</PId>
<Name>ABC</Name>
</Person>
<Person>
<PId>2</PId>
<Name>XYZ</Name>
</Person>
</Root>
As shown in above XML the only issue is with PId, it should have value 2.
Please help. Thanks.
Here's a relatively simple solution.
When this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*">
<Root>
<xsl:apply-templates select="Pax" />
</Root>
</xsl:template>
<xsl:template match="Pax">
<xsl:variable name="vPosition" select="position()" />
<Person>
<PId>
<xsl:value-of select="Id" />
</PId>
<Name>
<xsl:value-of select="/*/Travelers/*[$vPosition]/Name" />
</Name>
</Person>
</xsl:template>
</xsl:stylesheet>
...is applied to the original XML:
<Response>
<Pax>
<Id>1</Id>
</Pax>
<Pax>
<Id>2</Id>
</Pax>
<Travelers>
<Traveler>
<Name>ABC</Name>
</Traveler>
<Traveler>
<Name>XYZ</Name>
</Traveler>
</Travelers>
</Response>
...the wanted result is produced:
<Root>
<Person>
<PId>1</PId>
<Name>ABC</Name>
</Person>
<Person>
<PId>2</PId>
<Name>XYZ</Name>
</Person>
</Root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="Response">
<Root>
<xsl:for-each select="Travelers/Traveler">
<Person>
<xsl:variable name="index" select="position()" />
<Pid><xsl:value-of select="//Pax[$index]/Id"/></Pid>
<Name><xsl:value-of select="Name"/></Name>
</Person>
</xsl:for-each>
</Root>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/Response">
<Root>
<xsl:for-each select="Pax">
<xsl:variable name="pos" select="position()"/>
<Person>
<PId>
<xsl:value-of select="Id"/>
</PId>
<xsl:apply-templates select="//Travelers">
<xsl:with-param name="pos" select="$pos"/>
</xsl:apply-templates>
</Person>
</xsl:for-each>
</Root>
</xsl:template>
<xsl:template match="Travelers">
<xsl:param name="pos"/>
<xsl:for-each select="//Name">
<xsl:if test="position()=$pos">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>