xsl-list
[Top] [All Lists]

Re: [xsl] How to copy attribute value to text?

2016-07-07 01:44:16
On 07.07.2016 07:22, Kenneth Reid Beesley krbeesley(_at_)gmail(_dot_)com wrote:
If I start with an input XML document that contains mixed text with <word> 
elements like this:

        … this is just <word correction=“too”>to</word> funny

I’d like to write an XSLT stylesheet that yields as output

        … this is just <word origerror=“to”>too</word> funny

So in the output I effectively want (in the same <word> element) to

        1.  Set the value of a new attribute to the original text() value, and
        2.  Reset the text() value to be the value of the original @correction 
attribute

I’ve tried many variants of the following, so far without success.  I’m using 
SaxonHE9-7-0-6J;
it runs, but the results are not as expected/hoped.

I’ve tried matching the text() in a separate template, but I can’t seem to reference the attribute 
values of the parent node (i.e., <word>) of the text() and the parent node’s attributes.  E.g, the 
following doesn’t work for me, failing somehow in the
select=“../@correction”  reference.

<xsl:template match=“word[@correction]/text()”>
        <xsl:value-of select=“../@correction”/>
</xsl:template>


You can use

        <xsl:template match="@* | node()">
                <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                </xsl:copy>
        </xsl:template>
        
        <xsl:template match="word[@correction]/text()">
                <xsl:value-of select="../@correction"/>
        </xsl:template>
        
        <xsl:template match="word/@correction">
                <xsl:attribute name="origerror" select=".."/>
        </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>