xsl-list
[Top] [All Lists]

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

2014-08-12 12:30:18
A template that removes xml:space will probably do what you describe. For 
example:

    <xsl:template match="node() | @*">
        <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
    </xsl:template>
    
    <xsl:template match="@xml:space[string() = 'preserve' and parent::pre]"/>

The qualification here is to ensure that @xml:space is only removed from the 
pre element when the default value is used, so non-default or instances of 
xml:space on other elements will pass through.

This could be rewritten as a template matching element pre that copies 
everything except @xml:space. For example:

    <xsl:template match="pre">
        <xsl:copy>
            <xsl:copy-of select="@*[not(name() = 'xml:space' and string() = 
'preserve')]"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>


You might find other unintended changes such as other default or fixed 
attributes, and expansion of entities. Often such changes are not a concern, or 
can be controlled by using the output serialization options in XSLT and 
templates to filter attributes that have default values. In situations where 
unintended changes are not allowed I've found that VTD-XML works well.

Hope this helps,

Vincent
        

-----Original Message-----
From: dvint(_at_)dvint(_dot_)com 
[mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com] 
Sent: Tuesday, August 12, 2014 12:23 PM
To: xsl
Subject: [xsl] Rebuild an element without copying defaulted attributes?

I have a situation where I need to process 4,000 DITA topics to update them. 
I'm making updates which will then be included in the production data going 
forward. I need to modify certain elements and pass others through unchanged.

My problem is the unchanged.

So there is an element <pre> that has a defaulted @xml:space set to preserve. 
When I rebuild this element and recreate all the existing attributes my output 
now has a hard coded attribute setting.

So

    <pre ixia_locid="7">there's also the Duplicate Supplier Taxpayer ID Number 
Report</pre>

became

 <pre ixia_locid="7" xml:space="preserve">there's also the Duplicate Supplier 
Taxpayer ID Number Report</pre>

It seems like the only way I can avoid getting the defaulted attributes copied 
is to remove the reference to the DTD in all the topics. Is there any other way 
to accomplish this?

Currently I'm using this template:

<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>
--~----------------------------------------------------------------
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>