2011-07-22 45 views
7

我是一个XSLT新手,有一个简单的任务:XSLT:更改某些属性值

假设我有以下XML:

<Element1> 
    <Element2 attr1="1"/> 
</Element1> 
<Element1 attr1="2"/> 
<Element1> 
    <Element2 attr1="2"/> 
</Element1> 

我想将XML转换到相同的XML和一个变化:所有名为“attr1”的属性无论它们在哪里都必须被转换,例如“1”将是“A”并且“2”将是“X”,即i。即到

<Element1> 
    <Element2 attr1="A"/> 
</Element1> 
<Element1 attr1="X"/> 
<Element1> 
    <Element2 attr1="X"/> 
</Element1> 

我怎样才能做到这一点? 在此先感谢!

回答

8

您可以定义要替换和替换字符的字符,然后使用translate。 您可以使用此XSLT:

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

    <xsl:variable name="in">12</xsl:variable> 
    <xsl:variable name="out">AX</xsl:variable> 

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

    <xsl:template match="@attr1"> 
     <xsl:attribute name="attr1"> 
      <xsl:value-of select="translate(., $in, $out)"/> 
     </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 

另一种方式:

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

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

    <xsl:template match="@attr1"> 
     <xsl:choose> 
      <xsl:when test=". = '1'"> 
       <xsl:attribute name="attr1"> 
        <xsl:text>A</xsl:text> 
       </xsl:attribute> 
      </xsl:when> 
      <xsl:when test=". = '2'"> 
       <xsl:attribute name="attr1"> 
        <xsl:text>X</xsl:text> 
       </xsl:attribute> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

<xsl:template match="@attr1">将匹配所有属性attr1,然后使用xsl:choose你创建这个属性适当的值。

+0

为了适应运行定制,改变'的xsl:variable'到'XSL :param'。 –

8

你没有说出@ attr = 3时会发生什么,例如,如果不是所选内容中的一个,那么只需复制该值即可。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="@attr1"> 
    <xsl:attribute name="attr1"> 
    <xsl:choose> 
     <xsl:when test=". = 1"> 
     <xsl:text>A</xsl:text> 
     </xsl:when> 
     <xsl:when test=". = 2"> 
     <xsl:text>X</xsl:text> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="." /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:attribute> 
</xsl:template> 
</xsl:stylesheet> 
+0

如果这些值需要来自外部?这意味着如何使用外部映射文件,它具有“1” - >“A”和“2” - >“X”映射? – Amol

+0

@Amol然后您可以使用引用外部文档的[key function](http://www.w3schools.com/xsl/func_key.asp)来设置查找。 –

+0

我问[这](http://stackoverflow.com/questions/14065523/applying-templates-multiple-times-based-on-string-values-from-other-document)问题,以获得更多的清晰度。 – Amol

2

的另一种方法,使用document功能:

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

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

    <xsl:template match="@attr1"> 
     <xsl:attribute name="attr1"> 
      <xsl:value-of select="document('')//l:item[l:in = current()]/l:out"/> 
     </xsl:attribute> 
    </xsl:template> 

    <xml xmlns="local"> 
     <item> 
      <in>1</in> 
      <out>A</out> 
     </item> 
     <item> 
      <in>2</in> 
      <out>X</out> 
     </item> 
    </xml> 

</xsl:stylesheet> 
+0

对文档使用密钥查找不是更有效吗? –

0

XSLT低于2版本工作的:

<xsl:output method="xml" indent="yes"/> 

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

    <xsl:template match="@attr1[.='1']"> 
     <xsl:attribute name="attr1"> 
      <xsl:value-of select="replace(.,'1','A')"/> 
     </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="@attr1[.='2']"> 
     <xsl:attribute name="attr1"> 
      <xsl:value-of select="replace(.,'2','X')"/> 
     </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet>