Hello,
i'm new to xslt, just using it for 2weeks not. And now i'm stuck.
So first examples:
Input:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:namespace1="http://namespace" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" attribute1="some1" xmlns:M="mOptions" xmlns:N="nOptions">
<namespace1:mode name="Main">
<node0>0</node0>
<namespace1:parm name="node1" attribute2="some2" namespace1:type="type1">
<child1 name="a" id="1"/>
<child2 name="b" id="2"/>
</namespace1:parm>
<element name="node2" attribute2="some3" namespace1:type="type2">
<child6 name="a6" id="6"/>
<child7 name="b7" id="7"/>
</element>
<node id="55"/>
</namespace1:mode>
<M:extra1 id="1"/>
<M:extra2 id="2"/>
<M:extra6 id="6"/>
<M:extra7 id="7"/>
</xmi:XMI>
Wanted output:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:namespace1="http://namespace" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" attribute1="some1" xmlns:M="mOptions" xmlns:N="nOptions">
<namespace1:parm name="node1" attribute2="some2" namespace1:type="type1">
<child1 name="a" id="1"/>
<child2 name="b" id="2"/>
</namespace1:parm>
<M:extra1 id="1"/>
<M:extra2 id="2"/>
</xmi:XMI>
So what i need is to copy a specific node, in this example name=node1 and namespace1:type=type1, then remove all <M:extra.../> nodes that are not used in specific node. It can be that node will be name="node2" and namespace1:type=type2, but node name should become namespace1:parm.
Name will differ, but figure out that i can read the name from options file into a variable.
For now i'm trying to clean up the xmi using:
Code:
<xsl:template match="xmi:XMI|*[@namespace1:type='type1']">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
but getting:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:namespace1="http://namespace"
xmlns:xmi="http://www.omg.org/spec/XMI/20110701"
xmlns:M="mOptions"
xmlns:N="nOptions"
attribute1="some1">0<namespace1:parm name="node1" attribute2="some2" namespace1:type="type1"/>
</xmi:XMI>
somehow i get 0 and do not get children of wanted node. Have no clue how to clean up/keep some <M:extra.../> tags yet.
When i try
Code:
<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="xmi:XMI|*[@namespace1:type='type1']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xmi:XMI|*[@namespace1:type='type1']">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
im getting:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:namespace1="http://namespace"
xmlns:xmi="http://www.omg.org/spec/XMI/20110701"
xmlns:M="mOptions"
xmlns:N="nOptions"
attribute1="some1">some1<namespace1:mode name="Main">
<namespace1:parm name="node1" attribute2="some2" namespace1:type="type1">node1some2type1<child1 name="a" id="1"/>
<child2 name="b" id="2"/>
</namespace1:parm>
</namespace1:mode>
<M:extra1 id="1"/>
<M:extra2 id="3"/>
<M:extra6 id="6"/>
<M:extra7 id="7"/>
</xmi:XMI>
Somehow i'm getting node1some2type1 in between my specific node and some1<namespace1:mode name="Main"> and children and still do not have a clue how to remove <M:extra...> thinking maybe can be done with another template mapping used children or something.