2017-07-07 83 views
0

我需要在XSLT 2樣式表中做一系列的替換。例如,我需要用「bbb」替換所有出現的「aaa」,並用「ddd」替換所有的「ccc」。在正常replace通話方面,我應該有XSLT 2鏈替換?

<xsl:value-of select="replace(replace(text(), 'aaa', 'bbb'), 'ccc', 'ddd')"/> 

然而,情況是,我有幾百個這樣的更換對,爲此,我需要一個超長select屬性。我想我可以用例如一個Python腳本生成選擇字符串,但它會很難看。有沒有XSLT的東西可能像

<xsl:replace from="aaa" to="bbb"/> 
<xsl:replace from="ccc" to="ddd"/> 
… 

這樣XSLT更具可讀性和可維護性(以及機器可操作性)?

回答

1

你必須創建一個功能,可以取代每學期

XSLT 2.0

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:funct="http://something" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:template match="text()"> 
     <xsl:value-of select="funct:textreplace($replaces/replaces/rep, .)"/> 
    </xsl:template> 


    <xsl:param name="replaces"> 
     <replaces> 
      <rep from="aaa" to="bbb"/> 
      <rep from="ccc" to="ddd"/> 
     </replaces> 
    </xsl:param> 

    <xsl:function name="funct:textreplace" as="xs:string"> 
     <xsl:param name="reps" as="element(rep)*"/> 
     <xsl:param name="text" as="xs:string"/> 
     <xsl:choose> 
      <xsl:when test="not($reps)"> 
       <xsl:value-of select="$text"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="funct:textreplace($reps[position() gt 1], replace($text, $reps[1]/@from, $reps[1]/@to))"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:function> 

</xsl:stylesheet>