I’m trying to transform an XML document into another XML document. I’ve tried various approaches, none of which produced the desired target XML document.
I have a hypothetical input XML document like this:
<rows>
<row>
<name>ON</name>
<description/>
</row>
<row>
<name>NY</name>
<description/>
</row>
</rows>
I also have two lookups:
<loc:locations>
<loc:location>
<loc:code>ON</loc:code>
<loc:value>Ontario</loc:value>
</loc:location>
<loc:location>
<loc:code>NY</loc:code>
<loc:value>New York</loc:value>
</loc:location>
</loc:locations>
<des:descriptions>
<des:description>
<des:code>ON</des:code>
<des:value>Ontario is a province in Canada</des:value>
</des:description>
<des:description>
<des:code>NY</des:code>
<des:value>New York is a state in the USA</des:value>
</des:description>
</des:descriptions>
I would like the input XML to be transformed into this target XML:
<places>
<place>
<name>Ontario</name>
<description>Ontario is a province in Canada</description>
</place>
<place>
<name>New York</name>
<description>New York is a state in the USA</description>
</place>
</places>
What is the XSLT that would accomplish this? Thanks!
Just to clean-up things for readability, because you adopted namespace convention, you can also conveniently save all lookup data into a single variable. Also use
current()to accessnameof current node. Avoid//.