Reputation: 13
i´m desperately looking for a simple solution to my problem and hoping that someone out there is able to help :).
Problem:
Given is a xml document containing elements with attributes. I need to pick some element values, put those before the elements and remove the elements content afterwards using xslt. Here comes the tricky part. I need to do this only for elements which are not embedded in a certain other element such as <a>
.
Example:
<document>
<text>Some text <element attribute="123">"abc"</element> more text.</text>
<text>Lots of text...</text>
<a><element attribute="123">"abc"</element></a>
</document>
Transform to:
<document>
<text>Some text "abc" (<element attribute="123"></element>) more text.</text>
<text>Lots of text...</text>
<a><element attribute="123">"abc"</element></a>
</document>
My solution so far:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="element[not(ancestor::a)]">
<xsl:value-of select= "." />
<xsl:text> (</xsl:text>
<xsl:copy-of select= "." />
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
will generate the following:
<document>
<text>Some text "abc" (<element attribute="123">"abc"</element>) more text.</text>
<text>Lots of text...</text>
<a><element attribute="123">"abc"</element></a>
</document>
This is quite close, but not the wanted result. Now I need to remove the "abc" from the first element or even copy the element without its content, but I´m not able to and somehow stuck to my solution. Anyone here who is able to enlighten me?
Upvotes: 1
Views: 1506
Reputation: 167436
Instead of <xsl:copy-of select= "." />
you want
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
which does a shallow copy and copies the attributes.
Upvotes: 2