XSLT to generate nested and non-nested XML elements simultaneously

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying write an XSLT to generate an XMI file for UML with a profile applied. The XMI output file must look something like this:
<div id="x_preview
<div style="color:black; background-color:white
<pre><...>
<span style="color:blue <<span style="color:#a31515 uml<span style="color:blue :<span style="color:#a31515 Package <span style="color:red xmi:type<span style="color:blue =<span style="color:black "<span style="color:blue uml:Package<span style="color:black " <span style="color:red xmi:id<span style="color:blue =<span style="color:black "<span style="color:blue _111<span style="color:black "<span style="color:blue >
<...>
<span style="color:blue <<span style="color:#a31515 packagedElement <span style="color:red xmi:type<span style="color:blue =<span style="color:black "<span style="color:blue uml:Class<span style="color:black " <span style="color:red xmi:id<span style="color:blue =<span style="color:black "<span style="color:blue _222<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ownedAttribute <span style="color:red xmi:type<span style="color:blue =<span style="color:black "<span style="color:blue uml:Property<span style="color:black " <span style="color:red xmi:id<span style="color:blue =<span style="color:black "<span style="color:blue _333<span style="color:black " <span style="color:blue />
<span style="color:blue </<span style="color:#a31515 packagedElement<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 uml<span style="color:blue :<span style="color:#a31515 Package<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 MyProfile<span style="color:blue :<span style="color:#a31515 AAA <span style="color:red xmi:id<span style="color:blue =<span style="color:black "<span style="color:blue _444<span style="color:black " <span style="color:red base_Class<span style="color:blue =<span style="color:black "<span style="color:blue _222<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 MyProfile<span style="color:blue :<span style="color:#a31515 BBB <span style="color:red xmi:id<span style="color:blue =<span style="color:black "<span style="color:blue _555<span style="color:black " <span style="color:red base_Property<span style="color:blue =<span style="color:black "<span style="color:blue _333<span style="color:black "<span style="color:blue >
<...>
[/code]


This output corresponds to an input XML file that would look like this:
<div id="x_preview
<div style="color:black; background-color:white
<pre><...>
<span style="color:blue <<span style="color:#a31515 AAA <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue ...<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 BBB <span style="color:red id<span style="color:blue =<span style="color:black "<span style="color:blue ...<span style="color:black " <span style="color:blue />
<span style="color:blue </<span style="color:#a31515 AAA<span style="color:blue >
<...>
[/code]


Basically, the input type "AAA" is matched to a UML Class with the stereotype "AAA" applied to it, and "BBB" is matched to a UML Property with the stereotype "BBB" applied to it.
When the element AAA is matched, two actions are to be taken: Create the "<packagedElement />" line and the "<MyProfile:AAA />" line in the output.
When the element BB is matched, two actions are to be taken: Create the "<ownedAttribute />" line and the "<MyProfile:BBB />" line in the output.
I hope that the requirement is clear. My problem is that the matching happens in a nested fashion, but one of the actions is in the same nested order while the other is not. Is there a way to achieve this in XSLT? My XSLT currently looks like this (and can
only perform the nested action and not the non-nested action):

<div id="x_preview
<div style="color:black; background-color:white
<pre><...>
<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 template <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue AAATemplate<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 packagedElement <span style="color:red xmi:type<span style="color:blue =<span style="color:black "<span style="color:blue uml:Class<span style="color:black " <span style="color:red ... <span style="color:blue >
<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 for-each <span style="color:red select<span style="color:blue =<span style="color:black "<span style="color:blue input:BBB<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 call-template <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue BBBTemplate<span style="color:black " <span style="color:blue />
<span style="color:blue </<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 for-each<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 packagedElement<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 template<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 template <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue BBBTemplate<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ownedAttribute <span style="color:red xmi:type<span style="color:blue =<span style="color:black "<span style="color:blue uml:Property<span style="color:black " <span style="color:red ... <span style="color:blue />
<span style="color:blue </<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 template<span style="color:blue >
<...>
[/code]


If there is better way to generate a profile-applied-UML-XMI using XSLT please let me know. Thanks for looking!


View the full article
 
Back
Top