---
title: "使用 DiffDog 2010 比较 XML 模式"
date: "2009-12-10"
tags: 
  - "altova-xmlspy"
  - "diff-merge-tool"
  - "diffdog"
  - "file-comparison"
  - "mapforce"
  - "missionkit"
  - "v2010"
  - "xml-schema"
  - "xmlspy"
  - "xslt"
description: 探索 DiffDog 2010 如何通过新的工具，更有效地进行 XML Schema 比较，这些工具可以可视化差异、映射元素，并生成 XSL 转换。
---
Status: #blog

Tags:  #altova-xmlspy #diff-merge-tool #diffdog #file-comparison #mapforce #missionkit #v2010 #xml-schema #xmlspy #xslt

Categories: [Altova](/blog/zh/category/altova.md) 
# 使用 DiffDog 2010 比较 XML 模式

DiffDog 2010 包含一个强大的新工具，用于比较 XML 模式。XML 开发者和其他用户可以使用该工具来更新现有的 XML 数据文件，以适应 XML 模式的演变。本文将介绍该功能的一个示例场景。

在深入了解新功能之前，让我们先使用 [DiffDog 文件比较](https://www.altova.com/zh/diffdog/file-diff-tool.html) 功能，快速查看两个 XML 模式。当然，就像以前的版本一样，[DiffDog 2010](https://www.altova.com/zh/diffdog/diff-merge-tool.html) 的用户可以将 XML 模式作为 .xsd 文件进行比较，并以一种色彩编码、具有 XML 意识的格式显示差异。

[![DiffDog XML 模式文件比较视图](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image1_thumb.gif "DiffDog file comparison view of XML Schemas")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image1.gif) 

这是一种很好的方法，可以识别和管理 XML 模式之间的差异，尤其是在您需要审查行业标准 XML 模式的修订版本，这些版本会随着时间的推移而不断演变时。[有什么新内容](https://www.altova.com/zh/whatsnew.html) 在 DiffDog 2010 版本中，还提供了一个额外的功能 [XML 模式差异分析](https://www.altova.com/zh/diffdog/xml-schema-diff-tool.html) 该功能以图形化方式并排显示两个 XML 模式，自动识别相同的元素，并允许用户比较差异，并生成 XSL 转换，用于更新 XML 数据文件。以下是当我们使用新的 XML 模式比较功能，打开上述两个相同的 XML 模式时，最初呈现的视图。

[![DiffDog 最初的版本用于比较 XML 模式（XML Schemas）的差异](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image2_thumb.gif "Initial DiffDog XML Schema Differencing view of XML Schemas")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image2.gif) 

这两个 XML 模式的根元素会自动连接。我们可以点击工具栏中的“比较”按钮，从而自动连接两个 XML 模式中相同的元素。

[![DiffDog XML 差异比较工具](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image3_thumb.gif "DiffDog XML Differencing")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image3.gif) 

（当然，我们也可以通过右键菜单选择“比较 XML 模式”，或者在“差异与合并”菜单中选择“开始比较”，或者按下 F5 快捷键——DiffDog 提供了多种方式来完成相同的任务，您可以根据自己的喜好选择使用方式。）

接下来，我们可以通过手动连接两个 XML 模式中不同名称的元素之间的指针箭头，来实现元素的映射。在这个例子中，右侧 XML 模式版本的大部分修改，实际上只是给元素赋予了新的名称，这些新的名称在我们的企业内部分发 XML 模式和其数据文件时，会更加清晰易懂。

[![DiffDog 的 XML 模式差异比较视图，支持用户自定义的 XML 模式](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image4_thumb.gif "User-mapped XML Schemas in DiffDog XML Schema Differencing view")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image4.gif) 

当所有元素都已映射完成后，我们可以生成一个XSLT文件，用于根据左侧的XML模式，将现有的XML数据文件转换为右侧较新版本中的内容，从而反映更新。此功能旨在帮助XML开发人员摆脱手动编写和调试XSL转换的繁琐工作。

[![DiffDog 差异比较和合并菜单](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image4a_thumb.gif "DiffDog Diff and Merge Menu")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image4a.gif) 

以下是一个基于左侧 XML 模式的原始 XML 数据文件的示例，如在 [Altova XMLSpy](https://www.altova.com/zh/xmlspy.html) 中查看：

[![在 XMLSpy 软件中查看的 XML 数据文件](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image5_thumb.gif "XML data file viewed in XMLSpy")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image5.gif) 

以下是应用我们使用 DiffDog 2010 创建的 XSL 转换后生成的输出文件。请注意，其中“author”元素被替换为“writer”， “email”被替换为“feedback”，等等。

[![XSL 转换后的结果在 XMLSpy 中查看](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image6_thumb.gif "XSL output viewed in XMLSpy")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image6.gif) 

如果需要转换大量的现有XML文件，那么 [项目管理](https://www.altova.com/zh/xmlspy/xml-project-management.html) XMLSpy 的功能可以帮助我们自动化处理流程。 我们可以将外部文件夹添加到 XMLSpy 项目中。

[![XMLSpy 项目辅助窗口](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image7_thumb.gif "XMLSpy Project Helper Window")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image7.gif) 

通过XMLSpy的属性对话框，我们可以为每个项目文件夹设置默认值，包括用于验证的XML Schema、用于转换的XSL以及输出的目标位置。

[![XMLSpy 项目文件夹属性对话框](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image8_thumb.gif "XMLSpy project folder properties dialog")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image8.gif)

现在，我们可以在 XMLSpy 项目辅助窗口中选择输入文件夹，然后使用快捷键 F10 将其中的所有文件进行转换。在最初使用 DiffDog 映射 XML 模式元素时，我们没有将“出版物”元素连接到左侧，因为早期版本的模式中没有对应的元素。这意味着，当我们使用 XSLT 转换 XML 输入文件时，生成的输出将不包含“出版物”元素。如果“出版物”是一个必需的元素，我们可以使用 [Altova MapForce](https://www.altova.com/zh/mapforce.html) 来快速解决这个问题。

DiffDog 的一个选项是生成 MapForce 映射，而不是 XSLT。当我们选择此选项时，MapForce 将启动，并且我们的 DiffDog 映射已经作为新的 MapForce 设计加载，如图所示。

[![MapForce 新设计](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image10_thumb.gif "MapForce New Design")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image10.gif)

很容易通过添加一个常数值作为出版物元素的默认值来增强映射功能。

[![MapForce 增强版设计](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image11_thumb.gif "MapForce enhanced design")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image11.gif) 

现在，我们可以从 MapForce 中保存一个 XSL 文件，该文件会重用我们在 DiffDog 中最初设计的全部元素映射，并添加一个常量。当我们将新的 XSL 文件应用于转换原始 XML 数据文件时，我们会得到一个结果，其中包含了发布元素的默认值。

[![最终输出结果在 XMLSpy 软件中进行查看](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image12_thumb.gif "Final version of output viewed in XMLSpy")](https://www.altova.com/blog/images/ComparingXMLSchemaswithDiffDog2010_B452/image12.gif) 

这篇文章首先介绍了DiffDog 2010中新增的XML Schema比较功能。通过一个简单但典型的实际案例，我们很快发现，借助与XMLSpy和MapForce的紧密集成，可以轻松完成许多其他任务。

这三款工具以及更多工具，都可以在 [Altova MissionKit 2010](https://www.altova.com/zh/missionkit/software-development-tools.html) 中以优惠价格获得。该集成套件包含 XML、数据库和 UML 工具，旨在满足现代软件架构师和 XML 开发人员在开发和数据管理方面的多样化需求。 [点击此处](https://www.altova.com/zh/download/missionkit/software_development_tools_enterprise.html) 立即下载免费试用版！
