0

I am trying to convert a xml file to another using XSLT. As I am a quite newbie to this please help me to create xslt to transform below input xml file to output xml file. Thanks in advance.

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<title>
    <subtitle1>
        <OrganizationDetails>
            <OrgName>qwe</EmpNumber>
            <OrgAddress>asdfg 123</EmpName>
        </OrginzationDetails >
    </subtitle1>
    <subtitle2>
        <ResourceDetails>
            <EmpNumber>1</EmpNumber>
            <EmpName>xyz</EmpName>
        </ResourceDetails>
        <ResourceDetails>
            <EmpNumber>2</EmpNumber>
            <EmpName>abc</EmpName>
        </ResourceDetails>
        <ResourceDepartment>
            <EmpNumber>1</EmpNumber>
            <DepartmentNo>IT</DepartmentNo>
        </ResourceDepartment>
        <ResourceDepartment>
            <EmpNumber>2</EmpNumber>
            <DepartmentNo>SALES</DepartmentNo>
        </ResourceDepartment>
    </subtitle2>
</title>

Expected Output XML

<?xml version="1.0" encoding="UTF-8"?>
<title>
    <subtitle1>
        <OrginzationDetails>
            <OrgName>qwe</EmpNumber>
            <OrgAddress>asdfg 123</EmpName>
        </OrginzationDetails >
    </subtitle1>
    <subtitle2>
        <ResourceDetails>
            <EmpNumber>1</EmpNumber>
            <EmpName>xyz</EmpName>
        </ResourceDetails>

        <ResourceDepartment>
            <EmpNumber>1</EmpNumber>
            <DepartmentNo>IT</DepartmentNo>
        </ResourceDepartment>
    </subtitle2>
</title>

<title>
    <subtitle1>
        <OrginzationDetails>
            <OrgName>qwe</EmpNumber>
            <OrgAddress>asdfg 123</EmpName>
        </OrginzationDetails >
    </subtitle1>

    <subtitle2>
        <ResourceDetails>
            <EmpNumber>2</EmpNumber>
            <EmpName>abc</EmpName>
        </ResourceDetails>
        <ResourceDepartment>
            <EmpNumber>2</EmpNumber>
            <DepartmentNo>SALES</DepartmentNo>
        </ResourceDepartment>
    </subtitle2>
</title>
1
  • 5
    In general, you will want to utilize a key in order to resolve the cross-references in your input. Commented May 8, 2017 at 14:10

1 Answer 1

1

The essence of the solution is

<xsl:for-each-group select="/title/subtitle/*" group-by="EmpNumber">
  <title>
    <subtitle>
      <xsl:copy-of select="current-group()"/>
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for the solution.It Works!!..I have just modified input xml with one more sub-element for root tag tittle.It would be a great help for me if you could help me how to incorporate the same with above solution..Thanks in advance.. :)
Please do not change a question after it has been answered in such a way as to invalidate the answer. It makes things thoroughly confusing for people who come here later looking for an answer to a similar question. If you have supplementary questions, open a new question.
@Ajeet, I have no idea how the key function would be useful here.
@Michael Kay can be possible in key function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.