使用xml:choose
和xml: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>
希望有人可以告诉我(菜鸟)我在这里做错了吗?
答案 0 :(得分:2)
这是产生NaN
<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
有两个问题(其中一个可能是您过度简化了XML)
Job
元素下没有QtyInSecondUnit
元素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的内置模板将执行完全相同的操作。