TL;DR> > 在 JSON、YAML、TOML 统治配置世界的今天,有人重新提出了一个激进观点:XML 其实是一种廉价的领域特定语言(DSL)。本文探讨为什么 XML 在特定场景下可能比现代格式更合适,以及技术选择中的”复古主义”倾向。


📋 本文结构

  1. 那个引发争议的标题
  2. DSL 的本质是什么?
  3. XML 的 DSL 特性
  4. 为什么 XML fell out of favor?
  5. XML vs 现代格式:场景对比
  6. XML 作为 DSL 的真实案例
  7. 复古主义的工程智慧
  8. 结论:合适工具做合适的事

那个引发争议的标题

Reddit 上最近有一篇文章的标题很简单:

“XML is a Cheap DSL”

评论区瞬间分裂成两派:

  • 一派:”难道我们还要回到 SOAP 和 WSDL 的时代吗?”
  • 另一派:”等等,他说得其实有道理…”

最高赞评论很精辟:

“Imagine lisp but instead of parens you had xml tags”

(想象一下 Lisp,但用 XML 标签代替括号)

这个评论揭示了核心洞察:XML 和 Lisp 的 S-expression 在结构上是同构的


DSL 的本质是什么?

在讨论 XML 之前,我们需要理解什么是 DSL(Domain-Specific Language,领域特定语言)。

DSL 的特征

特征 说明
表达能力 用领域术语表达概念
约束性 不允许表达领域外的概念
可读性 领域专家能读懂
可验证性 可以静态检查正确性

例子对比

通用语言(Python)表达构建配置:

build_config = {
    "targets": [
        {
            "name": "myapp",
            "srcs": ["main.c", "util.c"],
            "deps": ["libfoo"],
            "copts": ["-O2", "-Wall"]
        }
    ]
}

DSL(Bazel BUILD 文件):

cc_binary(
    name = "myapp",
    srcs = ["main.c", "util.c"],
    deps = ["libfoo"],
    copts = ["-O2", "-Wall"],
)

DSL 更简洁,且可以在语法层面约束(比如 cc_binary 只接受特定参数)。


XML 的 DSL 特性

那么 XML 如何成为一种 DSL?

1. 结构表达能力

XML 的层级结构天然适合表达嵌套的领域概念:

<workflow name="order-processing">
    <step id="validate" type="validation">
        <input>order-data</input>
        <rules>
            <rule>amount > 0</rule>
            <rule>email valid</rule>
        </rules>
    </step>
    
    <step id="payment" type="action">
        <action>process-payment</action>
        <on-error goto="error-handler"/>
    </step>
    
    <step id="notify" type="notification">
        <template>order-confirmation</template>
    </step>
</workflow>

这比等效的 JSON 更具可读性:

{
  "workflow": {
    "name": "order-processing",
    "steps": [
      {
        "id": "validate",
        "type": "validation",
        "input": "order-data",
        "rules": ["amount > 0", "email valid"]
      },
      {
        "id": "payment",
        "type": "action",
        "action": "process-payment",
        "on-error": {"goto": "error-handler"}
      }
    ]
  }
}

2. 属性 vs 元素的语义区分

XML 允许你用属性表达元数据,用元素表达内容:

<book isbn="978-3-16-148410-0" published="2023">
    <title>The Art of Programming</title>
    <author>Donald Knuth</author>
</book>

这在 JSON 中会变得模糊:

{
  "book": {
    "isbn": "978-3-16-148410-0",
    "published": "2023",
    "title": "The Art of Programming",
    "author": "Donald Knuth"
  }
}

3. 命名空间和验证

XML 的 Schema(XSD)提供了强大的验证能力:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="workflow">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="step" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="id" type="xs:ID" use="required"/>
                        <xs:attribute name="type" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="validation"/>
                                    <xs:enumeration value="action"/>
                                    <xs:enumeration value="notification"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这比 JSON Schema 更早、更成熟、工具链更丰富。


为什么 XML fell out of favor?

Reddit 上有一条高赞评论解释了原因:

“The reason XML fell out of favor is precisely because it’s so complex and flexible. It’s difficult to parse and it’s never really clear if you should use attributes or elements, and the entire namespace concept for most people is totally irrelevant to what they’re trying to do yet the parsing libraries all force you to learn and care about it.”

XML 失宠正是因为它太复杂和灵活。

具体原因:

1. 过度工程的企业应用

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
                xmlns:m="http://www.example.org/stock">
    <soap:Header>
        <m:authToken>abc123</m:authToken>
    </soap:Header>
    <soap:Body>
        <m:GetStockPrice>
            <m:StockName>IBM</m:StockName>
        </m:GetStockPrice>
    </soap:Body>
</soap:Envelope>

对比 REST:

GET /stock/IBM
Authorization: Bearer abc123

2. 解析复杂性

XML 解析器需要处理:

  • 命名空间
  • CDATA
  • 实体引用
  • DTD(Document Type Definition)
  • 编码问题

而 JSON 解析器只需要处理:

  • 对象
  • 数组
  • 字符串
  • 数字
  • 布尔/null

3. 属性 vs 元素的困境

<!-- 哪种更好? -->
<book title="The Art of Programming" author="Donald Knuth"/>

<!-- 还是 -->
<book>
    <title>The Art of Programming</title>
    <author>Donald Knuth</author>
</book>

没有明确答案,导致团队内部争论。


XML vs 现代格式:场景对比

适合 XML 的场景

场景 原因
文档标记 HTML 的成功证明 XML-like 语法适合文档
复杂配置 需要验证和结构化编辑
遗留系统集成 已经大量使用 XML
需要混合内容 如技术文档(文本 + 代码 + 图片)
行业标准 如 Maven POM、Android Manifest

不适合 XML 的场景

场景 更好的选择
API 通信 JSON、gRPC
简单配置 YAML、TOML
脚本/代码 专用脚本语言
人类手写 YAML、TOML

XML 作为 DSL 的真实案例

1. Apache Maven

<project>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Maven 的 XML 配置就是一种 DSL:领域术语(dependency、scope、artifact)直接映射到 XML 元素。

2. Android 布局文件

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"/>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"/>
</LinearLayout>

Android 布局 DSL 直观且强大。

3. SVG

<svg width="100" height="100">
    <circle cx="50" cy="50" r="40" stroke="black" fill="red"/>
</svg>

SVG 就是 XML 作为图形 DSL 的完美例子。


复古主义的工程智慧

Reddit 上有一条幽默的评论:

“Everything that was old and crusty is the hottest rage. Bro let me tell you about soap and wsdl”

这句话背后是一种现象:工程师们开始重新审视被抛弃的老技术

为什么老技术会复兴?

  1. 问题循环:新问题往往是老问题的变种
  2. 过度矫正:从极端(SOAP)到另一个极端(REST),然后开始寻找中间地带
  3. 工具成熟:当年折磨我们的工具问题,现在可能已经解决
  4. 领域适应:某些领域(如配置管理)其实更适合老技术的特性

不是怀旧,是务实

拥抱 XML 不是怀旧,而是:

  • 承认 JSON/YAML 不是万能的
  • 根据场景选择工具
  • 不因为某个技术”过时”就自动排除它

结论:合适工具做合适的事

XML 是不是一个好的 DSL?

答案是:看情况

如果你需要:

  • ✅ 严格的结构验证
  • ✅ 层级嵌套表达
  • ✅ 混合内容(文本 + 标记)
  • ✅ 成熟的工具链(XSD、XPath、XSLT)

XML 可能是一个好选择。

如果你需要:

  • ❌ 人类可读/可写
  • ❌ 简单的键值配置
  • ❌ 低解析开销
  • ❌ Web API

选择 JSON/YAML/TOML。

正如那位 Reddit 用户所说:

“Please don’t.” 😂

但请记住:技术的价值在于解决问题,而不是追随潮流。

SOAP 可能确实过度工程了,但 XML 本身——作为一种结构化的数据表示方式——在某些场景下仍然有其价值。

合适工具做合适的事,无论它是新是旧。


参考与延伸阅读


本文灵感源自 2026-03-16 Reddit r/programming 讨论。

发布于 postcodeengineering.com