Title: Extracting related different element tags in an XML based on some equality condition on attributes values using XPATH and XSLT
Tags: xml;xslt;xpath;xslt-2.0;break
Question: I need to pull data out of XML in some specific way using XPATH and XSLT
<data&gt;
<person id="p1"&gt;
<name&gt;User1</name&gt;
</person&gt;
<person id="p2"&gt;
<name&gt;User2</name&gt;
</person&gt;
<person id="p3"&gt;
<name&gt;User3</name&gt;
</person&gt;
<employee eid="emp1" pid="p1"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
<employee eid="emp2" pid="p3"&gt;
<dept&gt;dept3</dept&gt;
</employee&gt;
<employee eid="emp3" pid="p2"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
</data&gt;
From the above example I need to create XMLs of each with the Person and the corresponding Employee elements in the output xml.  The link between these two xmls is
person.id = employee.pid
<person id="p1"&gt;
<name&gt;User1</name&gt;
</person&gt;
<employee eid="emp1" pid="p1"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
<person id="p2"&gt;
<name&gt;User2</name&gt;
</person&gt;
<employee eid="emp3" pid="p2"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
<person id="p3"&gt;
<name&gt;User3</name&gt;
</person&gt;
<employee eid="emp2" pid="p3"&gt;
<dept&gt;dept3</dept&gt;
</employee&gt;
I have tried many ways but not able to get this.
Thanks...

Here is the accepted answer: XPath is a query language for XML documents -- as such the evaluation of an XPath expression cannot modify an existing document or create a new XML document.
What you want can best be achieved using XSLT 2.0 (XSLT 1.0 if only one result document is needed):
<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output omit-xml-declaration="yes" indent="yes"/&gt;
<xsl:strip-space elements="*"/&gt;
<xsl:key name="kEmpByPid" match="employee" use="@pid"/&gt;
<xsl:template match="person"&gt;
<xsl:result-document href="file:///c:/temp/delete/XML{position()}"&gt;
<t&gt;
<xsl:copy-of select=".|key('kEmpByPid', @id)"/&gt;
</t&gt;
</xsl:result-document&gt;
</xsl:template&gt;
<xsl:template match="text()"/&gt;
</xsl:stylesheet&gt;
When this XSLT 2.0 transformation is applied on the provided XML document:
<data&gt;
<person id="p1"&gt;
<name&gt;User1</name&gt;
</person&gt;
<person id="p2"&gt;
<name&gt;User2</name&gt;
</person&gt;
<person id="p3"&gt;
<name&gt;User3</name&gt;
</person&gt;
<employee eid="emp1" pid="p1"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
<employee eid="emp2" pid="p3"&gt;
<dept&gt;dept3</dept&gt;
</employee&gt;
<employee eid="emp3" pid="p2"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
</data&gt;
the following three XML documents are created in "c:\temp\delete":
<t&gt;
<person id="p1"&gt;
<name&gt;User1</name&gt;
</person&gt;
<employee eid="emp1" pid="p1"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
</t&gt;
<t&gt;
<person id="p2"&gt;
<name&gt;User2</name&gt;
</person&gt;
<employee eid="emp3" pid="p2"&gt;
<dept&gt;dept1</dept&gt;
</employee&gt;
</t&gt;
<t&gt;
<person id="p3"&gt;
<name&gt;User3</name&gt;
</person&gt;
<employee eid="emp2" pid="p3"&gt;
<dept&gt;dept3</dept&gt;
</employee&gt;
</t&gt;

Here is another answer: I have got the below solution for my problem above.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output omit-xml-declaration="yes" indent="yes" /&gt;
<xsl:strip-space elements="*" /&gt;
<xsl:template match="//person"&gt;
<xsl:variable name="ID" select="@id" /&gt;
<xsl:result-document href="file:///c:/temp/delete/XML{position()}"&gt;
<t&gt;
<xsl:copy-of select=". | //employee[@pid=$ID]" /&gt;
</t&gt;
</xsl:result-document&gt;
</xsl:template&gt;
<xsl:template match="text()" /&gt;
</xsl:stylesheet&gt;
Just wanted to share with you.
Thanks.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:10/19/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xml><xslt><xpath><xslt-2.0><break>', 'Title': 'Extracting related different element tags in an XML based on some equality condition on attributes values using XPATH and XSLT', 'ViewCount': 154, 'id': 12980257, 'user_AboutMe': '', 'user_AccountId': 1957538.0, 'user_DisplayName': 'user1760178', 'user_Reputation': 6037.0, 'user_Views': 816.0, 'user_id': 1760178.0}}