xml attribute replacement with xslt

How can i replace an attribute in xml using xsl transformation, depending on its value. For example, if there is such xml

<Text Style='style1'>
...
</Text>

transform it to

<Text Font='Arial' Bold='true' Color='Red'>
...
</Text>

For Style='style2' set another attributes and values, for example Font='Sans' Italic='true'.

Answers


One posible way: ussing attribute sets. This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:attribute-set name="style1">
        <xsl:attribute name="Font">Arial</xsl:attribute>
        <xsl:attribute name="Bold">true</xsl:attribute>
        <xsl:attribute name="Color">Red</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set name="style2">
        <xsl:attribute name="Font">Sans</xsl:attribute>
        <xsl:attribute name="Italic">true</xsl:attribute>
    </xsl:attribute-set>
    <xsl:template match="Text[@Style='style1']">
        <xsl:copy use-attribute-sets="style1">
            <xsl:copy-of select="@*[name()!='Style']"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Text[@Style='style2']">
        <xsl:copy use-attribute-sets="style2">
            <xsl:copy-of select="@*[name()!='Style']"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

With this input:

<root>
    <Text Style='style1'></Text>
    <Text Style='style2'></Text>
</root>

Output:

<Text Font="Arial" Bold="true" Color="Red"></Text>
<Text Font="Sans" Italic="true"></Text>

Other way: inline "attribute sets". This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my"
 exclude-result-prefixes="my">
    <xsl:output indent="yes"/>
    <my:style1 Font="Arial" Bold="true" Color="Red"/>
    <my:style2 Font="Sans" Italic="true"/>
    <xsl:template match="Text[@Style]">
        <xsl:copy>
            <xsl:copy-of select="document('')/*/my:*
                                    [local-name()=current()/@Style]/@*"/>
            <xsl:copy-of select="@*[name()!='Style']"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Need Your Help

Flux Dispatcher - View actions vs. Server Actions

reactjs-flux

Is there any reason, other than semantics, to create different dispatch methods for view and server actions? All tutorials and examples I’ve seen (most notably this) ignore the source constant enti...

Python - Combine 2 masks arrays with same dimensions

arrays numpy combinations mask

I would like to combine two masked arrays of same dimensions. I work on a grid and i do calculation on two parts of the array defined with the grid.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.