xsl-list
[Top] [All Lists]

Re: [xsl] Rebuild an element without copying defaulted attributes?

2014-08-12 12:04:14

My problem is the unchanged.


<xsl:template match="*">
<xsl:element name="{name(.)}">
<xsl:for-each select="@*">
    <xsl:attribute name="{name(current())}" ><xsl:value-of
select="."/></xsl:attribute> </xsl:for-each> <xsl:apply-templates/>
</xsl:element> </xsl:template>

If you want to differentiate between an explicitly defaulted attribute in your 
input (<pre xml:space="preserve" />) and an implicitly defaulted attribute 
(<pre />) you are out of luck with standard XSLT. But if it works for you if 
you just make all attributes of xml:space implicitly defaulted, you can do the 
following:

<xsl:template match="node() |@*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="@xml:space[. = 'preserve']" />

Now the output will not contain any xml:space="preserve", which, if it is part 
of your DTD, has the same semantic meaning as copying the implicit default 
attributes which you did in your example code.

This is also a bit of a more convenient way to write the matching template you 
wrote, as the xsl:copy will automatically copy the context node, which you were 
doing manually (but: you were not processing the children, so my code and your 
code is not exactly the same).

Cheers,

Abel Braaksma
Exselt streaming XSLT 3.0 processor
http://exselt.net
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>