【发布时间】:2013-05-28 13:03:53
【问题描述】:
我正在尝试从前面的元素中检索值。但是我尝试检索的值需要在某个位置之后和其他节点位置之前。我该怎么做?
示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<code>tr</code>
<value>503</value>
</action>
<action>
<code>co</code>
<value>0</value>
</action>
<action>
<code>cou</code>
<value>0</value>
</action>
<action>
<code>tr</code>
<value>87</value>
</action>
<action>
<code>st</code>
<value>0</value>
</action>
<action>
<code>wta</code>
<value>0</value>
</action>
<action>
<code>pi</code>
<value>0</value>
</action>
<action>
<code>tr</code>
<value>64</value>
</action>
<action>
<code>st</code>
<value>0</value>
</action>
<action>
<code>del</code>
<value>0</value>
</action>
<action>
<code>tr</code>
<value>27</value>
</action>
<action>
<code>wa</code>
<value>0</value>
</action>
<action>
<code>dec</code>
<value>0</value>
</action>
</actions>
当前 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/actions">
<result>
<!-- Loop through all action elements -->
<xsl:for-each select="action">
<!-- Display only the needed action in the result file -->
<xsl:if test="code = 'co' or code = 'st' or code = 'dec' or code = 'pi' or code = 'del'">
<action>
<code>
<xsl:choose>
<xsl:when test="code = 'co'">1</xsl:when>
<xsl:when test="code = 'st'">5</xsl:when>
<xsl:when test="code = 'dec'">2</xsl:when>
<xsl:when test="code = 'pi'">3</xsl:when>
<xsl:when test="code = 'del'">4</xsl:when>
</xsl:choose>
</code>
<!-- Get some positions in variables -->
<xsl:variable name="previousPosition"><xsl:value-of select="position() - 1" /></xsl:variable>
<xsl:variable name="lastTRPosition"><xsl:value-of select="count((preceding::action[code = 'tr'])[last()]/preceding::action)+1" /></xsl:variable>
<xsl:variable name="currentPosition"><xsl:value-of select="position()" /></xsl:variable>
<!-- Should be the value of the preceding action element with code 'tr' (last occurence). But only use when between the last preceding action element with code 'tr' and the current node position NO known code is used ('co', 'st', 'dec', 'pi' or 'del') -->
<value>
<xsl:choose>
<xsl:when test="(preceding::action[code = 'tr']/value)[last()] != ''"> <!-- some work to do here -->
<xsl:value-of select="round((preceding::action[code = 'tr']/value)[last()])" />
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</value>
</action>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
当前结果:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<action>
<code>1</code>
<value>503</value>
</action>
<action>
<code>5</code>
<value>87</value>
</action>
<action>
<code>3</code>
<value>87</value>
</action>
<action>
<code>5</code>
<value>64</value>
</action>
<action>
<code>4</code>
<value>64</value>
</action>
<action>
<code>2</code>
<value>27</value>
</action>
</result>
想要的结果:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<action>
<code>1</code>
<value>503</value>
</action>
<action>
<code>5</code>
<value>87</value>
</action>
<action>
<code>3</code>
<value>0</value>
</action>
<action>
<code>5</code>
<value>64</value>
</action>
<action>
<code>4</code>
<value>0</value>
</action>
<action>
<code>2</code>
<value>27</value>
</action>
</result>
变化及原因?
<action>
<code>3</code>
<value>87</value> <!-- should be 0 -->
</action>
这应该是 0。因为在最后一个 action/code = 'tr' 的位置和要写入结果的节点的当前 position() 之间是一个已知的代码“st”,它已经具有这个值。
<action>
<code>4</code>
<value>64</value>
</action>
这应该是 0。因为在最后一个 action/code = 'tr' 的位置和要写入结果的节点的当前 position() 之间是一个已知的代码“st”,它已经具有这个值。
我在xsl:when 中获得正确的测试有点困难。有人可以帮忙吗?
【问题讨论】: