Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XML、XPATH和添加值_Java_Xml_Xpath - Fatal编程技术网

Java XML、XPATH和添加值

Java XML、XPATH和添加值,java,xml,xpath,Java,Xml,Xpath,我有XML文件,其中包含具有以下结构的记录: <xml> <payment contractType="1"> <income type="0"> <gr code="1" amount="1506.00"/> <gr code="4" amount="35.00"/> <gr code="10" amount="288.14"/> <de cod

我有XML文件,其中包含具有以下结构的记录:

<xml>
   <payment contractType="1">
     <income type="0">
       <gr code="1" amount="1506.00"/>
       <gr code="4" amount="35.00"/>
       <gr code="10" amount="288.14"/>
       <de code="3011300" amount="138.72"/>
       <de code="3081100" amount="48"/>
       <de code="3082400" amount="110"/>
     </income>
     <netAmount1 value="765.00"/>
    <netAmount2 value="765.00"/>
  </payment> 
 <payment contractType="1">
     <income type="0">
       <gr code="1" amount="506.00"/>
       <gr code="4" amount="35.00"/>
       <gr code="10" amount="0"/>
       <de code="3011300" amount="1.28"/>
       <de code="3081100" amount="48"/>
       <de code="3082400" amount="120"/>
     </income>
     <netAmount1 value="635.00"/>
    <netAmount2 value="635.00"/>
  </payment> 
</xml>

每个文件都有许多payment类型的记录,我希望最终的xml包含一个payment 通过添加每个不同代码值的所有金额值进行记录 上面xml的结果是

<xml>  
  <payment contractType="1">
     <income type="0">
       <gr code="1" amount="2512.00"/>
       <gr code="4" amount="70.00"/>
       <gr code="10" amount="288.14"/>
       <de code="3011300" amount="140"/>
       <de code="3081100" amount="96"/>
       <de code="3082400" amount="230"/>
     </income>
     <netAmount1 value="1400.00"/>
    <netAmount2 value="1400.00"/>
  </payment> 
</xml>

我认为XPath可以用于此,但我以前从未使用过它,有人能给我看一些Java(或其他)代码吗?

XPath的好例子:考虑到你的例子,你会很容易得到它。示例基于JavaScript
深入到您的问题,我认为您需要的是xslt转换,基于给定的xml创建另一个xml文件。是类似的例子,相当简化。要计算
金额的汇总,您需要执行以下操作:

sum(//gr/@amount)
更新: 嗯,我只是决定玩一玩。我所做的:

1) 以Eclipse IDE为例
2) 使用xml文件创建项目
3) 创建如下xsl文件:

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

        <totalpayment>

             <xsl:value-of select='sum(//@amount)'/>

        </totalpayment>

    </xsl:template>
</xsl:stylesheet>
我刚刚用您提供的xml执行了这个xsl,得到了您需要的相同结果:

<?xml version="1.0" encoding="UTF-8"?>
<payment contractType="1">
<income type="0">
<gr code="1" amount="2012"/>
<gr code="4" amount="70"/>
<gr code="10" amount="288.14"/>
<de code="3011300" amount="140"/>
<de code="3081100" amount="96"/>
<de code="3082400" amount="230"/>
<netAmount1 value="1400"/>
<netAmount2 value="1400"/>
</income>
</payment>

除了值
-我不知道为什么是2512.00


我不知道xslt是否已经准备好供您使用,或者它不是那么通用,但我希望这是一个好的开始

忘记添加“以下结构”?非常感谢。。当你谈论Eclipse时,你指的是简单的java版本??另一件事是,我不想要所有金额的总和,而是每个金额的总和根据代码属性…我使用Eclipse IDE面向Java EE开发人员,可能值得一提(可能是差异),关于您的结果-请编辑您的问题,并将您显示的xml的结果放在那里,因此,我们可以看到您想要的转换的正确结果perform@Vassilis很抱歉,一小时前刚注意到您的问题中有有效的输入/输出。可能是@dogbane编辑了你的问题并制作了它们visible@Vassilis我希望我复制了整个xslt:)如果这里有任何问题,我会在我们之后提供;祝你玩得愉快
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />
    <xsl:key name="names" match="//*" use="local-name(.)" />

    <xsl:template match="/">
        <payment contractType="1">
            <income type="0">
                <xsl:for-each select="//@code[not(.=preceding::*//@code)]">
                    <xsl:variable name="tagname">
                        <xsl:value-of select="name(//*[@code=current()])" />
                    </xsl:variable>

                    <xsl:element name="{$tagname}">
                        <xsl:attribute name="code"> 
                            <xsl:value-of select="current()" />
                        </xsl:attribute>
                        <xsl:attribute name="amount">
                            <xsl:value-of select="sum(//*[@code=current()]/@amount)" /><br />
                        </xsl:attribute>
                    </xsl:element>
                </xsl:for-each>

                <xsl:for-each select="//*[generate-id(.) = generate-id(key('names', local-name(.))) and starts-with(name(),'netAmount')]">
                    <xsl:variable name="tagname">
                        <xsl:value-of select="name()" />
                    </xsl:variable>

                    <xsl:element name="{name()}">
                        <xsl:attribute name="value">
                            <xsl:value-of select="sum(//*[name()=$tagname]/@value)"></xsl:value-of>
                        </xsl:attribute>
                    </xsl:element>
                </xsl:for-each>

            </income>
        </payment>
    </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<payment contractType="1">
<income type="0">
<gr code="1" amount="2012"/>
<gr code="4" amount="70"/>
<gr code="10" amount="288.14"/>
<de code="3011300" amount="140"/>
<de code="3081100" amount="96"/>
<de code="3082400" amount="230"/>
<netAmount1 value="1400"/>
<netAmount2 value="1400"/>
</income>
</payment>