2011-07-22 64 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>