XML 空标签被转换为开始和结束标签对 [英] XML empty tag being converted to open and closing tag pair

查看:27
本文介绍了XML 空标签被转换为开始和结束标签对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型 XML 主配置文件,其中包含许多互连应用程序的详细信息.大部分配置对所有应用程序都是通用的,但有些部分有所不同,例如 IP 地址、数据库连接字符串等.

I have a large XML master configuration file that contains details for a a number of inter connected applications. Much of the configuration is common to all applications but some pieces differ, such as IP addresses, database connection strings etc.

我已经将特定于特定应用程序的元素包含在一个 conditional 元素中,并且一个属性用于定义嵌套元素用于哪个应用程序.这允许我转换主配置文件以生成所需的应用程序特定配置文件.

I've enclosed the elements specific to a particular application within a conditional element and an attribute is used to define which application the nested elements are used for. This allows me to transform the master configuration file to generate the required application specific config file.

我遇到的问题是输出 XML 具有表单的空标记元素

The problem I'm having is the output XML has empty tag elements of the form

<add key="Setting3" value="1.2.3.4" />

改为

<add key="Setting3" value="1.2.3.4">
</add>

虽然我理解这无关紧要,但将生成的 XML 文件与当前所有手动创建的特定于应用程序的文件进行比较变得更加困难.我可以对我的 xslt 做些什么来保持结果中的空标签不变?

While I understand this should not matter, it makes it harder to diff the resulting XML file with the current app specific files which were all created by hand. Is there something I can do to my xslt that will keep the empty tags unchanged in the results?

我的xslt如下

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="app" select="''"></xsl:param>
  <xsl:output method="xml" indent="yes" version="1.0" encoding="utf-8"/>

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

  <xsl:template match="conditional">
    <xsl:if test="starts-with(@app,$app))">
      <xsl:copy-of select="*|@*|comment()|processing-instruction()"/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

示例主配置文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- Common Settings for all applications -->
  <add key="Setting1" value="1000" />
  <add key="Setting2" value="600" />

  <conditional app="App1">
    <!-- App1 Settings -->
    <add key="Setting3" value="1.2.3.4" />
  </conditional>

  <conditional app="App2">
      <!-- App2 Settings -->
      <add key="Setting3" value="1.3.10.10" />
  </conditional>
</configuration>

谢谢

按照 Martin 的建议更改 xml:output 改进了输出,因为结束标记现在附加到同一行的开始标记上,但我仍然希望将其保留为空源 XML 文件的标记格式.

Changing the xml:output as Martin suggested improves the output as the closing tag is now appended to the opening tag on the same line, but I'd still like to get it to keep the empty tag formatting of the source XML file.

所以

<add key="Setting3" value="1.2.3.4" />

变成

<add key="Setting3" value="1.2.3.4"></add>

我使用的是微软的 msxml.exe 3.0 版,如下

I am using Microsoft's msxml.exe version 3.0 as follows

msxml.exe master.config config.xslt app=App1 > App1.config
msxml.exe master.config config.xslt app=App2 > App2.config

如果有更好的处理器,我怀疑我可以切换到在 Windows 上运行的任何其他免费可用的处理器.

If there is a better processor I suspect it would be fine for me to switch to any other freely available processor that runs on Windows.

推荐答案

我认为问题不在于您展示的 XSLT(尽管您可以尝试是否使用 <xsl:output method="xml" indent="no" version="1.0" encoding="utf-8"/> 改进了一些东西),而是与您使用 XSLT 处理器运行转换的方式有关.因此,请考虑告诉我们您使用的是哪个 XSLT 处理器,以及您是否有运行转换的代码来向我们展示代码.

I think the problem is not with the XSLT you have shown (although you could try whether using <xsl:output method="xml" indent="no" version="1.0" encoding="utf-8"/> improves things) but rather with the way you run the transformation with your XSLT processor. So consider to tell us which XSLT processor you use and if you have code running the transformation to show us the code.

这篇关于XML 空标签被转换为开始和结束标签对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆