I’m a novice using XSLT. My requirement is best expressed through code. I’m looking for advice as to whether its possible to achieve my requirements through XLST. I’m not asking people to write code for me – although code advice would also be appreciated. I’m looking for pointers, e.g. “I need to chain multiple transforms”. I’m using XSLT v1.0.
I have the input XML;
<Input>
<BarId>123</BarId>
<BarName>myname</BarName>
<FooName>dummy</FooName>
</Input>
…and need to create the output;
<Out>
<Foos>
<Foo>
<Id>100</Id>
<Name>dummy</Name>
</Foo>
</Foos>
<Bars>
<Bar>
<Id>123</Id>
<Name>myname</Name>
<FooId>100</FooId>
</Bar>
</Bars>
</Out>
Key Point:
Creating the Foo element is the part I need help with.
The Foo element(s) most precede the Bar element(s).
I’m not overly concerned about generating the Foo Id (i.e. in above example 100) – I have a few options here – but I guess the approach for populating the FooId on the Bar will depend on the overall approach?
(ps. apologies for vague title – Ill update accordingly based on resolution)
I am assuming you want to output a
Fooelement with a unique ID for each distinctFooNamein the input. For this, you could use the Muenchian method:First you’d build a
xsl:keyusing which you can quickly retrieveFooNameelements by their content.Using this key, you can then process only the first instance of each FooName, thus getting a distinct list. Here you can make use of the
generate-id()function for comparing nodes for identity.For generating the identifiers, if you don’t care that the generated identifiers may vary from run to run, you can again use the
generate-id()function.Putting it all together:
With the following sample input:
It will yield the output: