一、本次 XSLT 2.0 改造:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<Employer>
<xsl:apply-templates/>
</Employer>
</xsl:template>
<xsl:template match="Employees">
<xsl:variable name="vNames" select="tokenize(Names, ' ')"/>
<xsl:variable name="vLoc" select="tokenize(Location, ' ')"/>
<xsl:variable name="vWeather"
select="tokenize(translate(Weather, '%', ' '), ' ')"/>
<xsl:for-each select="$vNames">
<xsl:variable name="vPos" select="position()" as="xs:integer"/>
<Employees>
<Names><xsl:sequence select="."/></Names>
<Location>
<xsl:sequence select="(lower-case($vLoc[$vPos]), 'Unknown')[1]"/>
</Location>
<Weather>
<xsl:sequence select="($vWeather[$vPos], 100)[1]"/>
</Weather>
</Employees>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<Employer>
<Employees>
<Names>vel bel sel tel mel</Names>
<Location>IND AUS ENG CAL JAP</Location>
<Weather>26%</Weather>
</Employees>
<Employees>
<Names>asd sadl asdsel tdddel dmdel</Names>
<Location>IND AUS ENG CAL JAP</Location>
</Employees>
</Employer>
产生想要的正确结果:
<Employer>
<Employees>
<Names>vel</Names>
<Location>ind</Location>
<Weather>26</Weather>
</Employees>
<Employees>
<Names>bel</Names>
<Location>aus</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>sel</Names>
<Location>eng</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>tel</Names>
<Location>cal</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>mel</Names>
<Location>jap</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>asd</Names>
<Location>ind</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>sadl</Names>
<Location>aus</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>asdsel</Names>
<Location>eng</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>tdddel</Names>
<Location>cal</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>dmdel</Names>
<Location>jap</Location>
<Weather>100</Weather>
</Employees>
</Employer>
请注意:
我做了以下合理的假设:
你其实想要100
,不是100%
。
您希望所有的都Employees
被处理——不仅仅是这个元素的第一次出现。
我还为任何缺失的位置添加了一个默认值,以防提供的位置数量少于提供的名称数量。
二、XSLT 1.0 解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:my="my:my" exclude-result-prefixes="ext my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:defaults>
<L>Unknown</L>
<W>100</W>
</my:defaults>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vDefaults" select="document('')/*/my:defaults"/>
<xsl:template match="/*">
<Employer>
<xsl:apply-templates/>
</Employer>
</xsl:template>
<xsl:template match="Employees">
<xsl:variable name="vrtfNames">
<xsl:apply-templates select="Names"/>
</xsl:variable>
<xsl:variable name="vNames" select="ext:node-set($vrtfNames)/*"/>
<xsl:variable name="vrtfLocs">
<xsl:apply-templates select="Location"/>
</xsl:variable>
<xsl:variable name="vrtfWeather">
<xsl:apply-templates select="Weather"/>
</xsl:variable>
<xsl:apply-templates select="$vNames">
<xsl:with-param name="pLocs" select="ext:node-set($vrtfLocs)/*"/>
<xsl:with-param name="pWeather" select="ext:node-set($vrtfWeather)/*"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="s" priority="3">
<xsl:param name="pLocs"/>
<xsl:param name="pWeather"/>
<xsl:variable name="vPos" select="position()"/>
<Employees>
<Names><xsl:value-of select="."/></Names>
<Location>
<xsl:value-of select=
"translate($pLocs[position() = $vPos]
| $vDefaults[not($pLocs[position() = $vPos])]/L,
$vUpper, $vLower)"/>
</Location>
<Weather>
<xsl:value-of select=
"$pWeather[position() = $vPos]
| $vDefaults[not($pWeather[position() = $vPos])]/W"/>
</Weather>
</Employees>
</xsl:template>
<xsl:template match="Weather">
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select="translate(., '%', ' ')"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="Employees/*/text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:variable name="vText" select="normalize-space($pText)"/>
<xsl:if test="$vText">
<s>
<xsl:value-of select="substring-before(concat($vText, ' '), ' ')"/>
</s>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select="substring-after($vText, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档(上图)时,同样会产生相同的正确结果:
<Employer>
<Employees>
<Names>vel</Names>
<Location>ind</Location>
<Weather>26</Weather>
</Employees>
<Employees>
<Names>bel</Names>
<Location>aus</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>sel</Names>
<Location>eng</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>tel</Names>
<Location>cal</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>mel</Names>
<Location>jap</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>asd</Names>
<Location>ind</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>sadl</Names>
<Location>aus</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>asdsel</Names>
<Location>eng</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>tdddel</Names>
<Location>cal</Location>
<Weather>100</Weather>
</Employees>
<Employees>
<Names>dmdel</Names>
<Location>jap</Location>
<Weather>100</Weather>
</Employees>
</Employer>
请注意:
基本上实现了与 XSLT 2.0 转换中相同的逻辑。
由于 XPath 1.0 没有 a tokenize
orlower-case()
函数并且在 XPath 1.0 数据模型中没有序列的概念,因此这些(分别)使用用于标记化的模板、使用translate()
转换为小写的函数和使用元素来实现包含天气和位置的默认值。