XSLT-使用xml时,无法从节点获取数据:

时间:2018-07-17 14:06:00

标签: xslt

使用xml:choosexml:when时,从节点获取数据时遇到问题。即使只从主xml文件中获得结果NaN或值。

XML文件的一部分:

<?xml version="1.0" encoding="UTF-8"?>
<Job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <Invoice>
    <InvoiceLine>
      <LineNo>1</LineNo>
      <QtyInSecondUnit>56</QtyInSecondUnit>
      <Quantity>56</Quantity>
      <CustTaric>
        <StatNo>34011100</StatNo>
        <IssuingCountry>GB</IssuingCountry>
      </CustTaric>
    </InvoiceLine>
    <InvoiceLine>
      <LineNo>2</LineNo>
      <QtyInSecondUnit>22</QtyInSecondUnit>
      <Quantity>0</Quantity>
      <CustTaric>
        <StatNo>44152020</StatNo>
        <IssuingCountry>GB</IssuingCountry>
      </CustTaric>
    </InvoiceLine>
  </Invoice>
</Job>

XSLT文件的一部分:

<?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">
        <xsl:apply-templates select="Job/Invoice"/>
      </xsl:template>

      <xsl:template match="Job/Invoice/InvoiceLine">
        <xsl:apply-templates select="QtyInSecondUnit"/>
        <xsl:apply-templates select="Quantity"/>
        <xsl:apply-templates select="CustTaric/StatNo"/>
      </xsl:template>

      <xsl:template match="QtyInSecondUnit">
        <xsl:choose>
          <xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
            <xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
          </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="."/>
          </xsl:otherwise>
        </xsl:choose>   
      </xsl:template>

      <xsl:template match="Quantity">
        <xsl:choose>
          <xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
            <xsl:value-of select="'0'"/>
          </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="."/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <xsl:template match="CustTaric/StatNo">
        <xsl:value-of select="."/>
      </xsl:template>

希望有人可以告诉我(菜鸟)我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

这是产生NaN

的行
<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>

有两个问题(其中一个可能是您过度简化了XML)

  1. 您使用的xpath表达式将相对于您所在的当前节点。当前Job元素下没有QtyInSecondUnit元素
  2. 问题中的XML中没有NetMass元素

假设NetMass确实存在于您的实际XML中,并且是父项InvoiceLine的子代,您想要的表达式就是这个

<xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>

您的xsl:when也可能有问题

 <xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">

这将测试文档中任何地方的任何CustTaric/StatNo。也许您只想测试当前InvoiceLine中的那个?如果是这样,请执行此操作...

<xsl:when test="../CustTaric/StatNo = '44152020'">

注意,您可以重写XSLT以将逻辑放入模板匹配项中,而不是xsl:choose

尝试使用此XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Job/Invoice/InvoiceLine">
    <xsl:apply-templates select="QtyInSecondUnit"/>
    <xsl:apply-templates select="Quantity"/>
    <xsl:apply-templates select="CustTaric/StatNo"/>
  </xsl:template>

  <xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/QtyInSecondUnit">
    <xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>
  </xsl:template>

  <xsl:template match="QtyInSecondUnit">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/Quantity">
    <xsl:value-of select="'0'"/>
  </xsl:template>

  <xsl:template match="Quantity">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="CustTaric/StatNo">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

严格来说,可以删除仅<xsl:value-of select="."/>的模板,因为如果XSLT中没有匹配的模板,则XSLT的内置模板将执行完全相同的操作。