下面是用Saxon 9.7 EE测试的XSLT 3.0样式表,它使用了
analyze-string
功能
parse-json
function
然后是新的
array
type
在XPath 3.1中。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="xs fn array">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="DOCUMENT/CONTENT/PHASE/INSTRUCTION/VALUE"/>
</xsl:template>
<xsl:template match="VALUE">
<dict>
<xsl:apply-templates select="analyze-string(., '\[.*\]', 's')//fn:match/parse-json(.)?*"/>
</dict>
</xsl:template>
<xsl:template match=".[. instance of array(xs:string)]">
<trans key="{.?1}" value="{.?2}"/>
</xsl:template>
</xsl:stylesheet>
它转换输入
<DOCUMENT>
.......
<CONTENT>
......
<PHASE>
......
<INSTRUCTION>
......
<VALUE><![CDATA[<script LANGUAGE="JavaScript" type="text/javascript">
arrayTitle = arrayTitle.concat([
["ÐÑбоÑ", "ÐÑÐ±Ð¾Ñ Ð¿Ð°ÑÑии"],
["ÐÑего на Ñкладе ÑÑаÑÑка", "ÐÑего на Ñкладе ÑÑаÑÑка"],
["â ÑеÑ
нолог. ÑеÑии", "â ÑеÑ
нолог. ÑеÑии"],
["ÐодпиÑÑ Ð¸ÑполниÑÐµÐ»Ñ /маÑÑеÑа", "ÐодпиÑѠиÑполниÑелÑ/ ÐодпиÑѠмаÑÑеÑа"],
["ÐоÑледн Ñмена (поÑледн ÑаÑÑ
од)", "ÐоÑледнее ÑпиÑание в ÑеÑии"]
]);</script>]]>
</VALUE>
......
</INSTRUCTION>
......
</PHASE>
......
</CONTENT>
......
</DOCUMENT>
结果
<?xml version="1.0" encoding="UTF-8"?>
<dict>
<trans key="ÐÑбоÑ" value="ÐÑÐ±Ð¾Ñ Ð¿Ð°ÑÑии"/>
<trans key="ÐÑего на Ñкладе ÑÑаÑÑка" value="ÐÑего на&nbspÑкладе ÑÑаÑÑка"/>
<trans key="â ÑеÑ
нолог. ÑеÑии" value="â&nbspÑеÑ
нолог. ÑеÑии"/>
<trans key="ÐодпиÑÑ Ð¸ÑполниÑÐµÐ»Ñ /маÑÑеÑа"
value="ÐодпиÑÑ&nbspиÑполниÑелÑ/ ÐодпиÑÑ&nbspмаÑÑеÑа"/>
<trans key="ÐоÑледн Ñмена (поÑледн ÑаÑÑ
од)"
value="ÐоÑледнее ÑпиÑание в&nbspÑеÑии"/>
</dict>
要使用与开源Saxon 9.7 HE相同的方法,我们可以使用
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="xs fn array">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="DOCUMENT/CONTENT/PHASE/INSTRUCTION/VALUE"/>
</xsl:template>
<xsl:template match="VALUE">
<dict>
<xsl:for-each select="analyze-string(., '\[.*\]', 's')//fn:match/parse-json(.)?*">
<tans key="{.?1}" value="{.?2}"/>
</xsl:for-each>
</dict>
</xsl:template>
</xsl:stylesheet>