XSLT-显示每行上的所有节点数据(应在不同的节点/行上拆分)

XSLT-显示每行上的所有节点数据(应在不同的节点/行上拆分),xslt,Xslt,我有个问题。无论我做什么,我都无法在每个节点上拆分NetMass。在输出中,所有NetMass在同一节点上依次显示。我想我忽略了一些简单明了的东西,但感谢所有的投入。仍在尝试教这个:)感谢所有的输入 XML的部分内容: <?xml version="1.0" encoding="UTF-8"?> <Job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <Invoice> <To

我有个问题。无论我做什么,我都无法在每个节点上拆分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>

18102.63
5248
1.
4358.15
2.
4358.15
3.
8461.71
4.
462.31
5.
462.31
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>

18102.63
5248
1.
1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314
4358.15
2.
1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314
4358.15
3.
1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314
公斤
8461.71
4.
1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314
462.31
5.
1263.4391356394071263.4391356394072453.0719613669394134.02488367712314134.02488367712314
462.31
想要这样:

<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>

18102.63
5248
1.
1263.439135639407
4358.15
2.
1263.439135639407
4358.15
3.
2453.0719613669394
公斤
8461.71
4.
134.02488367712314
462.31
5.
134.02488367712314
462.31

看来主要任务是计算
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++)上受支持)的完整示例在线:


在XSLT1中,我将其转录为

<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>

<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>