2012-01-02 47 views
3

我試圖定義一個實體,其值包含'+'字符,但如果我這樣做,我會得到一個奇怪的錯誤消息。如果我刪除+字符,一切工作正常。我似乎無法找出逃避它的辦法。
我得到的錯誤,不僅與庫我目前使用,也可與在線驗證在http://www.validome.org/grammar/validate/ 一個簡短的樣本:在DTD實體中使用特殊字符,如'+'

<?xml version="1.0" encoding="UTF-8"?> 

<!ENTITY % Foo "BAR"> <!--No problem here--> 
<!ENTITY % Baz "QUUX+QUUUX"> <!--This will cause trouble later on--> 

<!ENTITY % FooBazType "(%Foo; | %Baz;)"> 

<!ELEMENT tagName EMPTY> 
<!ATTLIST tagName attributeName %FooBazType; #REQUIRED> <!--Here, you get the error message : The enumerated type list must end with ')' in the "attributeName" attribute declaration.--> 

有誰知道的一種方式來獲得一個+字符(或某種方式也會正確地驗證在該位置包含+字符的XML文檔)在那裏?提前致謝!

+0

這標誌是有效的,它認爲這是試圖引用Unicode字符? – Joe 2012-01-02 16:41:38

+1

據我所知,我嘗試使用數字轉義序列(+),但那也不起作用。進一步的研究確實提出了NMTOKEN(http://www.w3.org/TR/xml/#NT-Nmtoken)的概念,這似乎排除了加...雖然我不相信我是第一個人遇到這個問題,因爲+角色不能那麼不尋常? – user1126518 2012-01-02 20:34:18

+0

我與'$'char(hex = 0x24)的問題完全相同 – Marek 2012-07-03 07:52:44

回答

1

問題不在於實體本身,而在於它用於定義屬性的事實,即legal values are enumerated。這些值必須匹配Nmtoken(一個或多個NameChar s)。這不包括'+'和'$',它們不是the definition of NameChar的一部分。下面的例子說明了這一點。

plus.dtd:

<!ELEMENT tagName EMPTY> 
<!ATTLIST tagName 
      attributeName (BAR | FOO+BAZ) #REQUIRED> 

plus.xml:

xmllint --dtdvalid plus.dtd plus.xml 
<?xml version="1.0"?> 
<tagName attributeName="FOO+BAZ"/> 
plus.dtd:2: parser error : ')' required to finish ATTLIST enumeration 
<!ATTLIST tagName attributeName (BAR | FOO+BAZ) #REQUIRED> 
             ^
plus.dtd:2: parser error : Space required after the attribute type 
<!ATTLIST tagName attributeName (BAR | FOO+BAZ) #REQUIRED> 
             ^
plus.dtd:2: parser error : Content error in the external subset 
<!ATTLIST tagName attributeName (BAR | FOO+BAZ) #REQUIRED> 
             ^
Could not parse DTD plus.dtd 

:試圖驗證plus.xml對plus.dtd時

<tagName attributeName="FOO+BAZ"/>  

xmllint輸出在固定的屬性值中使用'+'或'$'即可。

plus2.dtd:

<!ELEMENT tagName EMPTY> 
<!ATTLIST tagName 
      attributeName CDATA #FIXED "FOO+$BAZ"> 

xmllint輸出(無差錯):

xmllint --dtdvalid plus2.dtd plus.xml 
<?xml version="1.0"?> 
<tagName attributeName="FOO+$BAZ"/>