我需要做那样的事情
这是我的xml:
<TSRslt>
<TSExpectedResult Expected = "result : 42.7400131226"></TSExpectedResult>
<TSActual Actual = "result : 42.7400131226" Result = "0"></TSActual>
</TSRslt>
<TSRslt>
<TSExpectedResult Expected = "result :xyz"></TSExpectedResult>
<TSActual Actual = "result : xyz" Result = "1"></TSActual>
</TSRslt>
这是我的样式表xsl:
<TABLE id="table2" class="tl2" cellSpacing="0" cellPadding="2" width="100%">
<TR><TH noWrap = "noWrap" class="br">Expected</TH>
<TH noWrap = "noWrap" class="br">Actual</TH>
</TR>
<TR>
<xsl:for-each select="TSRslt">
<TR><TD class="br2" width="50%"><xsl:for-each select="TSExpectedResult"><xsl:value-of select="@Expected" /></xsl:for-each></TD>
<TD class="br2" width="50%"><xsl:for-each select="TSActual">
<xsl:if test="@Result = '0'">
<FONT COLOR = "#000000">
<xsl:value-of select="@Actual" />
</FONT>
</xsl:if>
<xsl:if test="@Result = '1'">
<FONT COLOR = "#000000">
<xsl:value-of select="@Actual" />
</FONT>
</xsl:if>
</xsl:for-each></TD>
</TR>
</xsl:for-each>
</TR>
</TABLE>
但是我的xml不是那样的。它的标签顺序如下:
<TSExpectedResult Expected = "result : -32.60900"></TSExpectedResult>
<TSExpectedResult Expected = "result : -32.60900"></TSExpectedResult>
<TSActual Actual = "result : -32.60900" Result = "0"></TSActual>
<TSActual Actual = "result : -32.60900" Result = "0"></TSActual>
我有太多的xml文件,因此我无法更改所有标签的订单。如何填写此订单的表格:
Expected Results Actual REsults
1 4
2 5
3 6
答案 0 :(得分:0)
似乎您希望访问与TSExpectedResult条目位置相同的TSActual条目。
因此,在TSExpectedResult上有一个for-each循环并访问TSActual条目
将当前位置放入变量:
<xsl:variable name="pos" select="position()" />
并使用它来访问TSActual条目:
select="../TSActual[position() = $pos]"
同样在以下示例中,我删除了不需要的厕所并使用了applay模板。
尝试这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match ="TSActual" mode="td">
<FONT COLOR = "#xxxx">
<xsl:attribute name="COLOR" >
<xsl:if test="@Result = '0'">#000001</xsl:if>
<xsl:if test="@Result = '1'">#000002</xsl:if>
</xsl:attribute>
<xsl:value-of select="@Actual" />
</FONT>
</xsl:template>
<xsl:template match="/*">
<TABLE id="table2" class="tl2" cellSpacing="0" cellPadding="2" width="100%">
<TR>
<TH noWrap = "noWrap" class="br">Expected</TH>
<TH noWrap = "noWrap" class="br">Actual</TH>
</TR>
<TR>
<xsl:for-each select="TSExpectedResult">
<xsl:variable name="pos" select="position()" />
<TR>
<TD class="br2" width="50%">
<xsl:value-of select="@Expected" />
</TD>
<TD class="br2" width="50%">
<xsl:apply-templates select="../TSActual[position() = $pos]" mode="td" />
</TD>
</TR>
</xsl:for-each>
</TR>
</TABLE>
</xsl:template>
</xsl:stylesheet>