aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSLT: reading value of field and creating elements in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT: reading value of field and creating elements in a loop" Watch "XSLT: reading value of field and creating elements in a loop" New topic
Author

XSLT: reading value of field and creating elements in a loop

Joe Dunbar
Greenhorn

Joined: Dec 30, 2013
Posts: 1
Hello,

this might be a simple question but I just started with xslt and i cant get it to work.

Basically I got an xml with this format coming in:
<?xml version="1.0" encoding="UTF-8"?>
<FileXML xmlns:vi="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/XSL/Transform">
<vi:Branch>
<vi:Identifier>81388</vi:Identifier>
<vi:Name>Union Square Limited</vi:Name>
<vi:BranchInformation>
<vi:BranchInformation>
<vi:Employees>3</vi:Employees>
</vi:BranchInformation>
</vi:BranchInformation>
</vi:Branch>
</FileXML>

What I am trying to achieve is an output like:

<?xml version="1.0" encoding="UTF-8"?>
<FileXML xmlns:vi="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/XSL/Transform">
<vi:Branch>
<vi:Identifier>81388-1</vi:Identifier>
<vi:Name>Union Square Limited</vi:Name>
<vi:BranchInformation>
<vi:BranchInformation>
<vi:Employees>1</vi:Employees>
</vi:BranchInformation>
</vi:BranchInformation>
</vi:Branch>
<vi:Branch>
<vi:Identifier>81388-2</vi:Identifier>
<vi:Name>Union Square Limited</vi:Name>
<vi:BranchInformation>
<vi:BranchInformation>
<vi:Employees>1</vi:Employees>
</vi:BranchInformation>
</vi:BranchInformation>
</vi:Branch>
<vi:Branch>
<vi:Identifier>81388-3</vi:Identifier>
<vi:Name>Union Square Limited</vi:Name>
<vi:BranchInformation>
<vi:BranchInformation>
<vi:Employees>1</vi:Employees>
</vi:BranchInformation>
</vi:BranchInformation>
</vi:Branch>
</FileXML>

This means it should read the amount of employees (=3) in the source xml and therefore create three Branch elements with all the original data but assign a unique Identifier. I think I can manage to change all the <vi:Employees> to have the value 1 afterwards but the rest is totally above my current skills.

Any thoughts or ideas are more than welcome!
Thanks!
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
I think I can manage to change all the <vi:Employees> to have the value 1 afterwards but the rest is totally above my current skills.

You mean you can change it to 1 and reproduce the original xml document as such with only that value changed? That seems already not that obvious for someone who just starts xslt. Could you show how you do it, the xslt document ? and from thereon I might help you to complete the task.

If you don't want to show the xslt that does that part, I can only say you make out a named template mainly based on that with a parameter initialized to 1. That named template would then call itself each time with the parameter increaded by 1 until the 3 is surpassed to stop. That is the big picture of the construction.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSLT: reading value of field and creating elements in a loop