2010-11-02 5 views
2

Comment puis-je remplacer un attribut dans XML en utilisant la transformation xsl, en fonction de sa valeur. Par exemple, s'il y a un tel xmlxml remplacement d'attribut avec xslt

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

transform à

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

Pour style = 'style2' définir un autre des attributs et des valeurs, par exemple la police = 'Sans' Italique = » vrai'.

Répondre

2

Une manière possible: utiliser des jeux d'attributs. Cette feuille de style:

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

Avec cette entrée:

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

Sortie:

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

Autre façon: en ligne "des ensembles d'attributs". Cette feuille de style:

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

Vous aurez besoin d'un type de règle que vous utiliserez pour convertir le style de l'un à l'autre. Faire l'hypothèse que vous entrez html vous aurez besoin de quelque chose comme.

<xsl:template match="@* | node()"> 
    <xsl:choose> 
     <xsl:when test="local-name() = 'Style'"> 
     <xsl:apply-templates select="." mode="Style" /> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template match="@Style" mode="Style"> 
    <xsl:choose> 
     <xsl:when test="node() = 'style1'"> 
     <xsl:attribute name="Font">Arial</xsl:attribute> 
     <xsl:attribute name="Bold">true</xsl:attribute> 
     <xsl:attribute name="Color">Red</xsl:attribute> 
     </xsl:when> 
     <xsl:when test="node() = 'style2'"> 
     <xsl:attribute name="Font">Sans</xsl:attribute> 
     <xsl:attribute name="Bold">true</xsl:attribute> 
     </xsl:when> 
    </xsl:choose> 
    </xsl:template>