here's a tricky one.
I have the following XML
<test>
<testElement SomeAttribute="<otherxml><otherElement>test test</otherElement></otherxml>">
</testElement>
</test>
Using XSLT, I want to transform this XML to have the following result.
<test>
<testElement>
<SomeAttributeTransformedToElement>
<otherxml>
<otherElement>test test</otherElement>
</otherxml>
</SomeAttributeTransformedToElement>
</testElement>
</test>
Basically, some text in an attribute must be transformed to actual elements in the final XML
Any ideas how to achieve that in XSLT?
Alex
I had the same need, and I finally managed to build something working.
However, it's far from perfect. But as this solution works for me (and is very useful in my case), I give it them to share with anyone who could be interrested too.
I hope that XSL-purists will forgive me for this :
This XSLT named-template takes a text variable as input, and generates XML elements on the output. Warning : There are some limitations :
So you've been warned : don't put this in production before having tested it and being sure that it's illegible to your case. Also, if you happen to fix or improve this script, please let me know.
Here are the templates :
And it's called with :
I hope this will be helful to at least one perso in the world (it was for me !)
You can achieve that by disabling output escaping. However, note that your input document is not a valid XML document (
<
is illegal in attribute values and needs escaping). I therefore changed your input document as follows:Input document
XSLT
Be aware that with
disable-output-escaping="yes"
there is no longer a guarantee that the produced output document is a well-formed XML document.