我有问题。无论我做什么,我都无法在每个节点上拆分NetMass。在输出中,所有NetMass都在同一节点上彼此显示。我想这只是一个简单明了的东西,我只是忽略了,但感谢所有的投入。仍在尝试教授此内容:)感谢您的所有输入!
XML的一部分:
<?xml version="1.0" encoding="UTF-8"?>
<Job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Invoice>
<TotalExVat>18102.63</TotalExVat>
<TotNetMass>5248.00</TotNetMass>
<InvoiceLine>
<LineNo>1</LineNo>
<NetMass></NetMass>
<AmountExVat>4358.15</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>2</LineNo>
<NetMass></NetMass>
<AmountExVat>4358.15</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>3</LineNo>
<NetMass></NetMass>
<AmountExVat>8461.71</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>4</LineNo>
<NetMass></NetMass>
<AmountExVat>462.31</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>5</LineNo>
<NetMass></NetMass>
<AmountExVat>462.31</AmountExVat>
</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:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Job">
<xsl:element name="Invoice">
<xsl:element name="TotalExVat">
<xsl:apply-templates select="Job/Invoice/TotalExVat"/>
</xsl:element>
<xsl:element name="TotNetMass">
<xsl:apply-templates select="Job/Invoice/TotNetMass"/>
</xsl:element>
<xsl:for-each select="Job/Invoice/InvoiceLine">
<xsl:element name="InvoiceLine">
<xsl:element name="NetMass">
<xsl:apply-templates select="NetMass"/>
</xsl:element>
<xsl:element name="AmountExVat">
<xsl:apply-templates select="AmountExVat"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="NetMass">
<xsl:variable name="NetFactor" select="number(translate(//Job/Invoice/TotNetMass,',','.')) div number(translate(//Job/Invoice/TotalExVat,',','.'))"/>
<xsl:for-each select="//Job/Invoice/InvoiceLine">
<xsl:value-of select="number(translate(AmountExVat,',','.')) * $NetFactor"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出:
<Job>
<Invoice>
<TotalExVat>18102.63</TotalExVat>
<TotNetMass>5248.00</TotNetMass>
<InvoiceLine>
<LineNo>1</LineNo>
<NetMass>1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314</NetMass>
<AmountExVat>4358.15</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>2</LineNo>
<NetMass>1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314</NetMass>
<AmountExVat>4358.15</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>3</LineNo>
<NetMass>1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314</NetMass>
<WeightUnit>KG</WeightUnit>
<AmountExVat>8461.71</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>4</LineNo>
<NetMass>1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314</NetMass>
<AmountExVat>462.31</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>5</LineNo>
<NetMass>1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314</NetMass>
<AmountExVat>462.31</AmountExVat>
</InvoiceLine>
</Invoice>
</Job>
想要这样:
<Job>
<Invoice>
<TotalExVat>18102.63</TotalExVat>
<TotNetMass>5248.00</TotNetMass>
<InvoiceLine>
<LineNo>1</LineNo>
<NetMass>1263.439135639407</NetMass>
<AmountExVat>4358.15</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>2</LineNo>
<NetMass>1263.439135639407</NetMass>
<AmountExVat>4358.15</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>3</LineNo>
<NetMass>2453.0719613669394</NetMass>
<WeightUnit>KG</WeightUnit>
<AmountExVat>8461.71</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>4</LineNo>
<NetMass>134.02488367712314</NetMass>
<AmountExVat>462.31</AmountExVat>
</InvoiceLine>
<InvoiceLine>
<LineNo>5</LineNo>
<NetMass>134.02488367712314</NetMass>
<AmountExVat>462.31</AmountExVat>
</InvoiceLine>
</Invoice>
</Job>
答案 0 :(得分:0)
似乎主要任务是计算NetMass
缺少的内容,因此我将为此编写一个模板,并使用单个单独的模板处理其余减去名称空间的复制:
<xsl:template match="*">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="NetMass">
<xsl:copy copy-namespaces="no">
<xsl:value-of select="ancestor::Invoice!(TotNetMass div TotalExVat) * ../AmountExVat"/>
</xsl:copy>
</xsl:template>
它使用XSLT 3(通过Saxon 9.8受广泛的平台(Java,.NET,C / C ++)支持),完整示例参见https://xsltfiddle.liberty-development.net/pPqsHTy:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:template match="*">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="NetMass">
<xsl:copy copy-namespaces="no">
<xsl:value-of select="ancestor::Invoice!(TotNetMass div TotalExVat) * ../AmountExVat"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在XSLT 1中,我将其转录为
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="NetMass">
<xsl:element name="{name()}">
<xsl:value-of select="ancestor::Invoice/TotNetMass div ancestor::Invoice/TotalExVat * ../AmountExVat"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>