RELAX NG 可用 XML 語法或緊湊語法描述。兩者語意相同,但緊湊語法通常更短、更易讀,適合撰寫與教學;XML 語法則較容易與既有 XML 工具鏈整合。
在許多實務案例中,緊湊語法能降低模式文件的視覺噪音,讓設計意圖更清楚。不過,若要在既有 XML 工具中直接驗證,仍常需要先轉換成 XML 語法。
緊湊語法的工具支援曾有一段時間不如 XML 語法完整。例如 trang 能在兩種語法之間轉換,但部分驗證工具主要針對 XML 語法模式。為了銜接不同工具鏈,開發者常會先用轉換工具產生 XML 語法,再進行驗證。
隨著工具演進,直接支援緊湊語法的情境逐漸增加。不過在跨團隊協作時,仍建議先確認工具鏈是否能處理所選語法,以免造成流程中斷。
緊湊語法可以用類似程式語言指派的方式宣告名稱空間。例如:
default namespace = "https://relaxng.org/ns/structure/1.0"
轉成 XML 語法時,這類宣告通常會反映在根元素的 xmlns 屬性上:
<root-tag xmlns="https://relaxng.org/ns/structure/1.0">
若要替元素或屬性宣告外部名稱空間,可以寫成:
namespace foo = "https://some.path.to/foo"
element foo:bar { ... }
對應的 XML 語法則會把名稱空間宣告放在根元素:
<root-tag xmlns="https://relaxng.org/ns/structure/1.0"
xmlns:foo="https://some.path.to/foo">
RELAX NG 允許注解。緊湊語法中,以兩個井號開頭的內容可視為注解:
## An annotation
轉成 XML 語法後,它會呈現為 documentation 元素:
<a:documentation>An annotation</a:documentation>
若以單一井號開頭,則是一般註解:
# This is a comment
對應的 XML 語法為:
<!-- This is a comment -->
若使用 a 命名空間的其他注解,緊湊語法也可能出現類似下面的寫法:
[ a:defaultValue = "foo" ]
在 XML 語法中,若使用注解,通常會自動帶入 a 命名空間;必要時也可自行指定:
namespace a = "https://relaxng.org/ns/compatibility/annotation/1.0"
RELAX NG 常搭配 W3C XML Schema 資料類型使用。緊湊語法可以這樣宣告:
datatypes xsd = "https://www.w3.org/2001/XMLSchema-datatypes"
轉成 XML 語法時,則會以 datatypeLibrary 屬性表示:
<root-tag xmlns="https://relaxng.org/ns/structure/1.0"
datatypeLibrary="https://www.w3.org/2001/XMLSchema-datatypes">
嵌套樣式會直接把元素與屬性放在它們應該出現的位置,結構直覺,適合表達單一元素的內容模型。
# A library patron example
default namespace = "https://some.other.url/ns"
namespace foo = "https://home.of.foo/ns"
datatypes xsd = "https://www.w3.org/2001/XMLSchema-datatypes"
## Annotation here
element patron {
element name { xsd:string } &
element id-num { xsd:string } &
element book {
( attribute isbn { text }
| attribute title { text }
| attribute anonymous { empty }) }*
}
在這個例子中,「&」表示元素必須出現,但順序不限;「|」表示可選其一。圓括號用來分組,'*' 則表示可重複零次或多次。
若需要對一組元素套用量化,可以使用群組:
element foo {
( element bar { text },
element baz { text } )+,
element bam { text }
}
這代表 foo 可以在最後的 bam 之前,包含一組或多組 bar 與 baz 的序列。
嵌套樣式也可以描述根元素的選項:
( element foo { text }
| element bar { text } )
另一種寫法較接近 DTD:先定義 start,再定義其他具名模式。這種方式適合表達遞歸結構,也能讓複雜模式更易於命名與重複使用。
# A library patron example
default namespace = "https://some.other.url/ns"
namespace foo = "https://home.of.foo/ns"
datatypes xsd = "https://www.w3.org/2001/XMLSchema-datatypes"
## Annotation here
start = patron
patron = name & id-num & book
name = element name { xsd:string }
id-num = element id-num { xsd:string }
book = element book {
( attribute isbn { text }
| attribute title { text }
| attribute anonymous { empty }) }*
這種寫法的好處是避免重複,並且能定義遞歸模式。例如 HTML 中常見的巢狀清單或巢狀表格,就可以用具名模式描述。
以下是上述上下文無關模式轉成 XML 語法後的樣貌:
<?xml version="1.0" encoding="UTF-8"?>
<!-- A library patron example -->
<grammar xmlns="https://relaxng.org/ns/structure/1.0"
ns="https://some.other.url/ns"
datatypeLibrary="https://www.w3.org/2001/XMLSchema-datatypes"
xmlns:a="https://relaxng.org/ns/compatibility/annotation/1.0"
xmlns:foo="https://home.of.foo/ns">
<a:documentation>Annotation here</a:documentation>
<start><ref name="patron"/></start>
<define name="patron">
<interleave>
<ref name="name"/>
<ref name="id-num"/>
<ref name="book"/>
</interleave>
</define>
<define name="name">
<element name="name">
<data type="string"/>
</element>
</define>
<define name="id-num">
<element name="id-num">
<data type="string"/>
</element>
</define>
<define name="book">
<zeroOrMore>
<element name="book">
<choice>
<attribute name="isbn"/>
<attribute name="title"/>
<attribute name="anonymous">
<empty/>
</attribute>
</choice>
</element>
</zeroOrMore>
</define>
</grammar>
相較於 XML 語法,緊湊語法通常更短,也更接近人類閱讀模式的方式。不過,兩者在語意上仍然一致,選擇哪一種主要取決於工具支援與團隊維護習慣。
在緊湊語法中,屬性主體通常需要明確指定 text 或 empty,以避免不確定性。屬性的量化通常只有「?」有意義,因為屬性不可重複多次。
不同轉換工具在注解位置、註解處理或空白呈現上可能有細微差異,但這些差異通常不影響語意。實務上仍建議在轉換後進行驗證,以確保結果符合預期。