File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joe Dunbar
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 649
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic