Transforming attribute values based on a dictionary encoded as a tree variable

pegasus

It should be apparent what I'm trying to achieve looking at these sample files:

Input:

<root>
    <node type="A"/>
    <node type="B"/>
</root>

Stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="fixes">
        <fix from="A" to="AA"/>
        <fix from="B" to="BB"/>
    </xsl:variable>

    <xsl:template match="@type[string()=$fixes/fix/@from]">
        <xsl:attribute name="type">
                <xsl:value-of select="$fixes/fix[@from=self::node()]/@to"/>
        </xsl:attribute>
    </xsl:template>

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

</xsl:stylesheet>

Expected output:

<root>
    <node type="AA"/>
    <node type="BB"/>
</root>

Actual output (using saxon9he):

<root>
    <node type=""/>
    <node type=""/>
</root>

Any idea what I'm doing wrong?

Martin Honnen

Replace self::node() with current() to select the @type attribute matched on (and not the context node of the predicate, which is a fix element).

In general for such tasks I would suggest to define a key

<xsl:key name="fix" match="fix" use="@from"/>

and then rewrite

as

<xsl:template match="@type[key('fix', ., $fixes)]">
  <xsl:attribute name="{name()}" select="key('fix', ., $fixes)/@to"/>
</xsl:template>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Transforming attribute values into xml element

XSLT Transforming XML into a cross referenced, nested HTML lists when source nodes are siblings and nesting is based on attribute values

How to transforming values from the list using a dictionary

Python Dictionary Contains Encoded Values

How to loop through a dictionary and assign values to a variable based on keys?

Transforming an XML file that contains logical operators in attribute values..?

Transform XML to XML using XSLT transforming attribute values on target XML

'DataFrame' object has no attribute 'ravel' when transforming target variable?

Transforming hash values based on condition after using group_by

Transforming a dictionary column into a dataframe

Transforming DataFrame to dictionary of dictionaries

Transforming list into dictionary with Python

Transforming Dictionary into pandas DF

Filter dictionary based on the values in second dictionary

Remove dictionary values based on regex?

Return dictionary name based on values

Sort mutable dictionary based on values of it

Sort Python Dictionary based on Values

Intersection of Dictionary based on keys and values

Averaging the values in a dictionary based on the key

Creating .csv based on values in dictionary

Order a dictionary based on specific values

Create a column based on values in a dictionary

Append to dictionary based on list values

Replace array values based on dictionary

Constraining attribute values based on another attribute

group objects based on their attribute values

field calculator based on attribute values

Create Dictionary with Variable Name Values