XPATH / XSL:无法使用节点集检索节点元素

时间:2018-03-28 10:28:09

标签: xml xslt xpath

我正在尝试将两个表连接在一起并输出一个表。 表“常规显示”保存值“BASICSA”,需要匹配保存值数据的表“所有费率”。

下面是我的xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="ConvDispData.xsl"?>
<Rates xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <TableList>
    <Table name="Conventional Display">
      <Tbody>
        <Tr>
          <Td>BASICSA </Td>
        </Tr>
      </Tbody>
    </Table>
    <Table name="All Rates">
      <Tbody>
        <Tr>
          <Td>BASICSA   </Td>
          <Td>balanceLimit001  </Td>
          <Td>rateamou1   </Td>
        </Tr>
      </Tbody>
    </Table>
  </TableList>
</Rates>

以下是我的xsl

<?xml version="1.0"  encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ext="http://exslt.org/common" 
   exclude-result-prefixes="ext msxsl"
>

    <xsl:output method="xml" />

    <xsl:variable name="TABLE_NAME">All Rates</xsl:variable>
    <xsl:variable name="ALL_RATES_TR" select="//Table[@name=$TABLE_NAME]/Tbody/Tr" />

    <xsl:variable name="CONV_TABLE_NAME">Conventional Display</xsl:variable>
    <xsl:variable name="TITLE1">Conventional Display</xsl:variable>
    <xsl:variable name="COLUMN1">WT-RATE-NUMBER</xsl:variable>
    <xsl:variable name="COLUMN2">WT-AMOUNT-TO-CHECK(1)</xsl:variable>
    <xsl:variable name="COLUMN3">WT-SPLIT-TIER-RATE(1)</xsl:variable>

    <xsl:variable name="REC_PER_PAGE">10</xsl:variable>

    <xsl:template match="/">

        <Rates>
            <TableList>
                <Table name="{$CONV_TABLE_NAME}"  cellspacing="0" cellpadding="0" class="tblDeposit">
                    <RecordPerPage>
                        <xsl:value-of select="$REC_PER_PAGE"/>
                    </RecordPerPage>
                    <xsl:call-template name="BuildHeader"/>
                    <Tbody>
                        <xsl:apply-templates select="//Table[@name=$CONV_TABLE_NAME]/Tbody/Tr">
                        </xsl:apply-templates>
                    </Tbody>
                </Table>
            </TableList>
        </Rates>
    </xsl:template>

    <xsl:template name="BuildHeader">
        <Thead>

            <Tr>
                <Td class="tdDepositHeaderType">
                    <xsl:value-of select="$COLUMN1"/>
                </Td>
                <Td class="tdDepositHeaderRates">
                    <xsl:value-of select="$COLUMN2"/>
                </Td>
                <Td class="tdDepositHeaderRates">
                    <xsl:value-of select="$COLUMN3"/>
                </Td>
            </Tr>
        </Thead>
    </xsl:template>

    <xsl:template match="Tr">
        <xsl:param name="PreClass"/>
        <xsl:variable name="Position">
            <xsl:value-of select="position()"/>
        </xsl:variable>

        <xsl:variable name="Class">
            <xsl:choose>
                <xsl:when test="$PreClass=''">
                    <xsl:call-template name="SectionClass">
                        <xsl:with-param name="Pos">
                            <xsl:value-of select="$Position"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$PreClass"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>


        <xsl:variable name="RATEID" select="Td[1]"/>
            <xsl:variable name="MATCHEDTD" select="ext:node-set($ALL_RATES_TR)/Tbody" />
            <Tr>
                <xsl:attribute name="Class">
                    <xsl:value-of select="$Class"/>
                </xsl:attribute>
                <Td class="tdDepositType">
                    <xsl:value-of select="$RATEID"/>
                </Td>

                <Td class="tdDepositType">
                    <xsl:value-of select="MATCHEDTD[2]"/>
                </Td>
                <Td class="tdDepositType">
                    <xsl:value-of select="MATCHEDTD[3]"/>
                </Td>
            </Tr>
    </xsl:template>

    <xsl:template name="SectionClass">
        <xsl:param name="Pos"/>
        <xsl:choose>
            <xsl:when test="$Pos mod 2 != 0">TOdd</xsl:when>
            <xsl:otherwise>TEven</xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

我对此代码的问题在于:

使用LiquidStudio,我可以看到,如果我执行ext:node-set($ ALL_RATES_TR),我会获得一系列值为Tbody的值。但是每当我尝试使用任何XPath来检索Tbody的更精细值时,选择将返回空。

我不明白为什么它可以返回空,即使路径应该是正确的。

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

您实际上并不需要在此使用node-set。您的$ALL_RATES_TR引用了输入文档中的节点,因此已经是节点集。

此外,$ALL_RATES_TR会为表格选择Tr元素,因此尝试在其下选择TBody是没有意义的。您可能意味着选择Td就像这样......

<xsl:variable name="MATCHEDTD" select="$ALL_RATES_TR/Td" />

完成此操作后,另一个问题是您在尝试访问$时忘记添加MATCHEDTD前缀。当你应该这样做时,你正在做<xsl:value-of select="MATCHEDTD[2]"/> ......

<xsl:value-of select="$MATCHEDTD[2]"/>