我們有一個程序使用xml來保存我們程序的配置。有人決定在我們的數據庫中重新命名一些值,這些重命名現在也應該在我們客戶的配置中向後兼容。用xslt重命名一批值
配置
<configuration>
<fruitToEat>yellow_curved_thing</fruitToEat> <!-- should now become banana -->
</configuration>
一個簡單的匹配將被(未測試,只是一個例子)的一個例子:
<xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match"/configuration/fruitToEat/text()">
<xsl:text>banana</xsl:text>
</xsl:template>
</xsl:template>
但是,這只是一個示例,並且我想這樣做150倍。
是否有可能使xsl讀取一個簡單的文本文件或ini文件,告訴我150個匹配應該看起來相似?
<xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- recreate this template 150 times from an ini file or something -->
<xsl:template match"/configuration/fruitToEat/text()[.='yellow_curved_thing']">
<xsl:text>banana</xsl:text>
</xsl:template>
</xsl:template>
我的映射文件的一個例子可能是簡單的:
yellow_curved_thing = banana
round_thing = tomato
round_dotted = strawberry
而且我只是想一個小XSLT,告訴我:
<xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- recreate this template 150 times from an ini file or something -->
<xsl:template match"/configuration/fruitToEat/text()[.=$fileRow0]">
<xsl:text>$fileRow1</xsl:text>
</xsl:template>
</xsl:template>
如果將「ini文件」視爲xml文件,這應該不是什麼大問題。使用'select =「document('ini_file.xml')..」'來訪問這個過濾器。但是你的請求很難模糊地知道這是否合理,或者如果在xlst中明確實現這個規則會更好。 –
所以我將不得不做一個新的XML,可以簡單地使所有規則的xslt顯式? – Marnix