【问题标题】:XSLT Need to add value to same element in repeating nodesXSLT 需要为重复节点中的相同元素添加值
【发布时间】:2021-08-18 22:47:32
【问题描述】:

我正在尝试创建 XSLT 以向下面的每个 Quantity 元素添加 10

  <Lines>
    <JournalLine>
      <ItemCode>40006061</ItemCode>
      <Quantity>90</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40006031</ItemCode>
      <Quantity>120</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40008331</ItemCode>
      <Quantity>2400</Quantity>
    </JournalLine>
  </Lines>

预期结果:-

  <Lines>
    <JournalLine>
      <ItemCode>40006061</ItemCode>
      <Quantity>100</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40006031</ItemCode>
      <Quantity>130</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40008331</ItemCode>
    <Quantity>2410</Quantity>
    </JournalLine>
  </Lines>

到目前为止,我所做的一切都导致在所有节点中重复相同的值,感谢任何帮助。

编辑:
我正在使用 XSLT-2.0,到目前为止,这是我的 XSLT 代码:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />
    
    <xsl:template match="node()|@*">         
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Quantity/text()">
        <xsl:value-of select = "format-number(//Quantity, '###,###.00')"/>
    </xsl:template>
    
    <xsl:template match="Filename"></xsl:template>
    
</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    您可以使用此 XSLT-1.0/2.0 代码来实现您的目标:

    <?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" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />
        
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        
        <xsl:template match="JournalLine/Quantity">
            <xsl:copy>
              <xsl:value-of select="format-number(.+10, '###,###.00')"/>
            </xsl:copy>
        </xsl:template>
        
    </xsl:stylesheet>
    

    它的输出是:

    <?xml version="1.0" encoding="utf-8"?>
    <Lines>
        <JournalLine>
          <ItemCode>40006061</ItemCode>
          <Quantity>100.00</Quantity>
        </JournalLine>
        <JournalLine>
          <ItemCode>40006031</ItemCode>
          <Quantity>130.00</Quantity>
        </JournalLine>
        <JournalLine>
          <ItemCode>40008331</ItemCode>
          <Quantity>2,410.00</Quantity>
        </JournalLine>
    </Lines>
    

    此输出将使用您在问题中指定的小数点格式化数字。

    【讨论】:

    • 谢谢,太好了,我忘了我在诊断时删除了 +10,感谢您这么快回来
    • 你的大错是//Quantity 选择了文档中的每个Quantity 元素。尽量避免使用//,除非在极少数情况下确实需要它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2018-12-18
    • 2018-12-30
    • 1970-01-01
    相关资源
    最近更新 更多