当前位置: 首页 > 知识库问答 >
问题:

比较两个XML文件中的价格

蒲深
2023-03-14
<xml>
  <produkt>
    <code>000161</code>
    <name>Test name</name>
    <available>Yes</available>
    <price>19,90</price>
  </produkt>
</xml>
<xml>
  <produkt>
    <code>000161</code>
    <name>Test name</name>
    <available>Yes</available>
    <price>18,90</price>
  </produkt>
</xml>
  • 产品由
  • 标识
  • 检查产品价格是否改变,如果是,复制元素(仅在本例中)
  • 不检查或复制新产品(不存在于older.xml:不复制)

compare.xml

<xml>
  <produkt>
    <code>000161</code>
    <name>Test name</name>
    <available>Yes</available>
    <oldprice>18,90</oldprice>
    <newprice>19,90</newprice>
  </produkt>
</xml>

共有1个答案

鲁华灿
2023-03-14

older.xml

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <produkt>
        <code>000160</code>
        <name>Product A (only in old list)</name>
        <available>Yes</available>
        <price>9,90</price>
    </produkt>
    <produkt>
        <code>000161</code>
        <name>Product B (price falls)</name>
        <available>Yes</available>
        <price>19,90</price>
    </produkt>
    <produkt>
        <code>000163</code>
        <name>Product D (price rises)</name>
        <available>Yes</available>
        <price>24,90</price>
    </produkt>
    <produkt>
        <code>000164</code>
        <name>Product E (price unchanged)</name>
        <available>Yes</available>
        <price>99,90</price>
    </produkt>
</xml>

newer.xml

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <produkt>
        <code>000161</code>
        <name>Product B (price falls)</name>
        <available>Yes</available>
        <price>18,90</price>
    </produkt>
    <produkt>
        <code>000162</code>
        <name>Product C (only in new list)</name>
        <available>Yes</available>
        <price>16,90</price>
    </produkt>
    <produkt>
        <code>000163</code>
        <name>Product D (price rises)</name>
        <available>Yes</available>
        <price>28,90</price>
    </produkt>
    <produkt>
        <code>000164</code>
        <name>Product E (price unchanged)</name>
        <available>Yes</available>
        <price>99,90</price>
    </produkt>
</xml>

compare.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

    <xsl:output indent="yes"/>

    <xsl:template match="xml">
        <xsl:variable name="older" select="document('older.xml')"/>
        <xml>
            <xsl:for-each select="produkt">
                <xsl:variable name="code" select="code"/>
                <xsl:variable name="findProduktByCode" select="$older/xml/produkt[code = $code]"/>
                <xsl:if test="price != $findProduktByCode/price">
                    <produkt>
                        <xsl:copy-of select="code"/>
                        <xsl:copy-of select="name"/>
                        <xsl:copy-of select="available"/>
                        <oldprice>
                            <xsl:value-of select="$findProduktByCode/price"/>
                        </oldprice>
                        <newprice>
                            <xsl:value-of select="price"/>
                        </newprice>
                    </produkt>
                </xsl:if>
            </xsl:for-each>
        </xml>
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xml>
   <produkt>
      <code>000161</code>
      <name>Product B (price falls)</name>
      <available>Yes</available>
      <oldprice>19,90</oldprice>
      <newprice>18,90</newprice>
   </produkt>
   <produkt>
      <code>000163</code>
      <name>Product D (price rises)</name>
      <available>Yes</available>
      <oldprice>24,90</oldprice>
      <newprice>28,90</newprice>
   </produkt>
</xml>
 类似资料:
  • 我在Visual Studio中看到了新的比较工具 有没有一种方法,我可以只是比较两个文件与内置的功能在Visual

  • 我试图比较两个xml文件并显示差异,如果找到的话。 目前,我正在使用XML Diff来查找差异。 也查看了xnode.deepeQuals,但它也返回是否有任何区别。但不是什么区别。 任何帮助都很感激

  • 我试图比较两个XML文件。我的要求是比较新旧xml文件,如果有任何差异,将其合并到新的xml文件中。 但我也想要差异。请告诉我怎样才能得到不同之处。 我已经尝试过XMLUnit,但我不想使用它。

  • 有两个叫做“a.txt”和“b.txt”的文件都有单词列表。现在我想检查哪些单词在“a.txt”中是额外的,而不是在“b.txt”中。 我需要一个有效的算法,因为我需要比较两个字典。

  • 问题内容: 有两个名为 “ a.txt” 和 “ b.txt”的文件 ,都有一个单词列表。现在,我要检查 “ a.txt”中 哪些单词是多余的,而 “ b.txt”中 哪些单词不是。 我需要一种有效的算法,因为我需要比较两个字典。 问题答案: 这是我的解决方案:

  • 我想比较visual studio中的两个文件(包含存储过程的文件),所以我想忽略文件中所有的差异。 我打开命令窗口,使用但它也考虑了案例差异 我已经看过这个问题和答案了