Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
使用xslt将同一结构化xml与total on属性合并_Xml_Xslt - Fatal编程技术网

使用xslt将同一结构化xml与total on属性合并

使用xslt将同一结构化xml与total on属性合并,xml,xslt,Xml,Xslt,我有两个相同结构的xml,我只想要最后一个xml中属性的和。与下面的input1.xml和input2.xml类似,atribute数据集的值为20,input2的值为30,因此output.xml的值应为50 input1.xml <?xml version="1.0" encoding="UTF-8"?> <chart xmlns=" " labelStep="1" showValues="0" anchorRadius="1"

我有两个相同结构的xml,我只想要最后一个xml中属性的和。与下面的input1.xml和input2.xml类似,atribute数据集的值为20,input2的值为30,因此output.xml的值应为50

input1.xml

<?xml version="1.0" encoding="UTF-8"?>
<chart xmlns=" "
       labelStep="1"
       showValues="0"
       anchorRadius="1"
      >
   <categories>
      <category Label="Technical RATH"/>
      <category Label="Technical RATH2"/>
   </categories>
   <dataset>
      <set value="20" color="a5cf5a"/>
      <set value="10" color="b5cf5a"/>
   </dataset>
</chart>

input2.xml

<?xml version="1.0" encoding="UTF-8"?>
<chart xmlns=" "
       labelStep="1"
       showValues="0"
       anchorRadius="1"
      >
   <categories>
      <category Label="Technical RATH"/>
      <category Label="Technical RATH2"/>
   </categories>
   <dataset>
      <set value="30" color="a5cf5a"/>
      <set value="150" color="b5cf5a"/>
   </dataset>
</chart>

和output.xml应该是

<?xml version="1.0" encoding="UTF-8"?>
<chart xmlns=" "
       labelStep="1"
       showValues="0"
       anchorRadius="1"
      >
   <categories>
      <category Label="Technical RATH"/>
      <category Label="Technical RATH2"/>
   </categories>
   <dataset>
      <set value="50" color="a5cf5a"/>
      <set value="160" color="b5cf5a"/>
   </dataset>
</chart>

您需要在这些文档中解决名称空间的问题,并根据需要调整以下内容,但抛开这些,您可以这样做:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="set/@value">
    <xsl:attribute name="value">
        <xsl:value-of select="number(.) + number(document('source2.xml')/chart/dataset/set/@value)"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

您需要在这些文档中解决名称空间的问题,并根据需要调整以下内容,但抛开这些,您可以这样做:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="set/@value">
    <xsl:attribute name="value">
        <xsl:value-of select="number(.) + number(document('source2.xml')/chart/dataset/set/@value)"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>


感谢您的回复,我在@smj“”上尝试了这一点。如果source2.xml具有相同的标记,如何设置这两个值,我尝试了-each,但没有得到结果我尝试了在第一个xml中的每个节点上使用,但得到了正确的输出,我觉得不是在读第二个xml文件我是这样做的谢谢你的回复,我在@smj上尝试了这个方法如果我们有相同标记的source2.xml,如何设置这两个值,我尝试了-每个,但没有得到结果我尝试了在第一个xml中的每个节点上使用,但得到了正确的输出,我感觉不是在读第二个xml文件,而是在这样做”