[英]XSLT space separated and adding an element
我在xml和xslt中有一個查詢
以下是輸入XML
<?xml version="1.0" encoding="UTF-8"?>
<Employer>
<Employees>
<EmployeesDetails>van ind 26%</EmployeesDetails>
</Employees>
<Employees>
<EmployeesDetails>van ind</EmployeesDetails>
</Employees>
</Employer>
以上是我的輸入文件
以下是我的輸出文件
<?xml version="1.0" encoding="UTF-8"?>
<Employer>
<Employees>
<Names>van</Names>
<Location>ind</Location>
<Weather>26</Weather>
</Employees>
<Employees>
<Names>van</Names>
<Location>ind</Location>
<Weather>100</Weather>
</Employees>
</Employer>
如何將下面的XSLT應用於上面的XML輸入?
I.此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沒有tokenize
或lower-case()
函數,並且在XPath 1.0數據模型中沒有序列的概念,因此這些(分別)是使用tokenize
模板來實現的,使用translate()
函數可以轉換為小寫字母,並使用包含天氣和位置默認值的元素。
您的問題在某些關鍵領域含糊不清。 例如,您似乎聲明,如果<Employees>
節點集沒有 <weather>
節點,則它應該得到一個值為100%的節點集; 也就是說,您的預期輸出似乎不一致地應用了該邏輯。 您所需的<Location>
結果節點將從大寫轉換為小寫。 此外,您的輸出似乎完全忽略了源XML中的第二個<Employees>
節點集。
進行一些假設,這是使用EXSLT的XSLT 1.0解決方案。 如果這不是您想要的,請更新您的問題以更具體,我將盡力解決。
當此XSLT:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:exsl="http://exslt.org/common"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="exsl"
version="1.0">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employees">
<xsl:variable name="vNames">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="Names/text()"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vLocations">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="Location/text()"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($vNames)/token">
<xsl:with-param name="pLocation"
select="exsl:node-set($vLocations)/token"/>
<xsl:with-param name="pWeather" select="Weather"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="token">
<xsl:param name="pLocation"/>
<xsl:param name="pWeather"/>
<xsl:variable name="vPosition" select="position()"/>
<Employees>
<Names>
<xsl:value-of select="."/>
</Names>
<Location>
<xsl:value-of select="translate($pLocation[$vPosition],
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
</Location>
<xsl:choose>
<xsl:when test="$pWeather != ''">
<xsl:apply-templates select="$pWeather"/>
</xsl:when>
<xsl:otherwise>
<Weather>100%</Weather>
</xsl:otherwise>
</xsl:choose>
</Employees>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text,$delimiter)">
<xsl:element name="token">
<xsl:value-of select="substring-before($text,$delimiter)"/>
</xsl:element>
<xsl:call-template name="tokenize">
<xsl:with-param name="text"
select="substring-after($text,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$text">
<xsl:element name="token">
<xsl:value-of select="$text"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
...應用於此XML:
<?xml version="1.0"?>
<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>
...產生所需的(?)結果:
<?xml version="1.0" encoding="UTF-8"?>
<Employer>
<Employees>
<Names>vel</Names>
<Location>ind</Location>
<Weather>26%</Weather>
</Employees>
<Employees>
<Names>bel</Names>
<Location>aus</Location>
<Weather>26%</Weather>
</Employees>
<Employees>
<Names>sel</Names>
<Location>eng</Location>
<Weather>26%</Weather>
</Employees>
<Employees>
<Names>tel</Names>
<Location>cal</Location>
<Weather>26%</Weather>
</Employees>
<Employees>
<Names>mel</Names>
<Location>jap</Location>
<Weather>26%</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>
說明:
<Employee>
元素匹配的第二個模板運行一個特殊的tokenize
模板,該模板的工作是將以空格分隔的字符串拆分為結果樹片段。 為了方便起見,將這些片段保存到變量中。 token
元素。 這些由EXSLT生成,它將結果樹片段轉換為由<token>
元素組成的節點集。 對於其中的每一個,我們提取必要的元素值以創建<Names>
和<Location>
。 該模板還包含確定<weather>
元素所需值的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.