在 XML 标签中表示空格和制表符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/514635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Represent space and tab in XML tag
提问by balaweblog
How to represent space and tab in XML tag. Is there any special characters for them to represent.
如何在 XML 标签中表示空格和制表符。他们有没有什么特殊的字符可以代表。
回答by galets
I think you could use an actual space or tab directly in XML document, but if you are looking for special characters to represent them so that text processors can't mess them up, then it's:
我认为您可以直接在 XML 文档中使用实际的空格或制表符,但是如果您正在寻找特殊字符来表示它们,以便文本处理器不会将它们弄乱,那么它是:
space =  
tab = 	
回答by Wasim A.
Work for me
对我来说有效
\n = 

\r = 
\t = 	
space =  
Here is an example on how to use them in XML
这是一个关于如何在 XML 中使用它们的示例
<KeyWord name="hello	" />
回答by kjhughes
New, expanded answer to an old, commonly asked question...
对一个旧的、常见的问题的新的、扩展的答案......
Whitespace in XML Component Names
XML 组件名称中的空格
Summary:Whitespace characters are notpermitted in XML element or attribute names.
摘要:空白字符不会在XML元素或属性名称中不允许。
Here are the main Unicode code points related to whitespace:
以下是与空格相关的主要 Unicode 代码点:
#x0009CHARACTER TABULATION#x0020SPACE#x000ALINE FEED (LF)#x000DCARRIAGE RETURN (CR)#x00A0NO-BREAK SPACE[#x2002-#x200A]EN SPACE through HAIR SPACE#x205FMEDIUM MATHEMATICAL SPACE#x3000IDEOGRAPHIC SPACE
#x0009字符表#x0020空间#x000A换行 (LF)#x000D回车 (CR)#x00A0不间断空间[#x2002-#x200A]EN SPACE 通过 HAIR SPACE#x205F中等数学空间#x3000表意空间
Noneof these code points are permitted by the W3C XML BNF for XML names:
没有这些代码点被允许W3C XML BNF的XML名称:
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040] Name ::= NameStartChar (NameChar)*
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040] Name ::= NameStartChar (NameChar)*
Whitespace in XML Content(Not Component Names)
XML 内容中的空格(不是组件名称)
Summary:Whitespace characters are, of course, permitted in XML content.
摘要:空白字符是的,当然,在允许的XML内容。
All of the above whitespace codepoints are permitted in XML content by the W3C XML BNF for Char:
W3C XML BNFChar允许在 XML 内容中使用上述所有空白代码点:
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
Unicode code points can be inserted as character references. Both decimal &#decimal;and hexadecimal &#xhex;forms are supported.
Unicode 代码点可以作为字符引用插入。支持十进制&#十进制;和十六&#x进制十六进制;形式。
- Hexadecimal Decimal Unicode Name
	or	CHARACTER TABULATION
or LINE FEED (LF)
or CARRIAGE RETURN (CR) or SPACE or NO-BREAK SPACE
回答by David Hanak
You cannot have spaces and tabs in the tag(i.e., name) of an XML elements, see the specs: http://www.w3.org/TR/REC-xml/#NT-STag. Beside alphanumeric characters, colon, underscore, dash and dot characters are allowed in a name, and the first letter cannot be a dash or a dot. Certain unicode characters are also permitted, without actually double-checking, I'd say that these are international letters.
XML 元素的标签(即名称)中不能有空格和制表符,请参阅规范:http: //www.w3.org/TR/REC-xml/#NT-STag。除了字母数字字符外,名称中还允许使用冒号、下划线、破折号和点字符,第一个字母不能是破折号或点。某些 unicode 字符也是允许的,无需仔细检查,我会说这些是国际字母。
回答by bobince
If you are talking about the issue where multiple and non-space whitespace characters are stripped specifically from attribute values, then yes, encoding them as character references such as 	 will fix it.
如果您正在谈论从属性值中专门去除多个和非空格空格字符的问题,那么是的,将它们编码为字符引用,例如 将修复它。
回答by user3584056
For me, to make it work I need to encode hex value of space within CDATA xml element, so that post parsing it adds up just as in the htm webgae & when viewed in browser just displays a space!. ( all above ideas & answers are useful )
对我来说,为了使它工作,我需要在 CDATA xml 元素中编码空间的十六进制值,以便后解析它就像在 htm webgae 中一样加起来,在浏览器中查看时只显示一个空格!。(以上所有想法和答案都很有用)
<my-xml-element><![CDATA[ ]]></my-xml-element>
回答by jlo-gmail
Illegal XML Tag Name Characters can be encoded using Unicode UCS-2. This works very nicely. I am using it to create XML that gets turned into json (JPath is weak compared to XPath). Notice the handling of spaces, (, ) characters.Unicode UCS-2 Code Chart: http://www.columbia.edu/kermit/ucs2.html
非法 XML 标记名称字符可以使用 Unicode UCS-2 进行编码。这很好用。我正在使用它来创建变成 json 的 XML(与 XPath 相比,JPath 很弱)。注意空格、 (, ) 字符的处理。Unicode UCS-2 代码表:http: //www.columbia.edu/kermit/ucs2.html
tag.Name = tag.Name.Replace(" ", "_x0020_");
tag.Name = tag.Name.Replace("(", "_x0028_");
tag.Name = tag.Name.Replace(")", "_x0029_");
XML:
XML:
<Internal_x0020_Chargeback_x0020_ID>{CHARGEBACKCODE}</Internal_x0020_Chargeback_x0020_ID>
<Bill_x0020_To>{CHARGEBACKCODE}</Bill_x0020_To>
<Operator_x0020_or_x0020_Directly_x0020_Responsible_x0020_Individual_x0020__x0028_DRI_x0029_>[email protected]</Operator_x0020_or_x0020_Directly_x0020_Responsible_x0020_Individual_x0020__x0028_DRI_x0029_>
transformed to json via json.net:
通过 json.net 转换为 json:
"Internal Chargeback ID": "{CHARGEBACKCODE}",
"Bill To": "{CHARGEBACKCODE}",
"Operator or Directly Responsible Individual (DRI)": "[email protected]",
回答by Martin Pfeffer
I had the same issue and none of the above answers solved the problem, so I tried something very straight-forward: I just putted in my strings.xml\n\t
我遇到了同样的问题,上面的答案都没有解决问题,所以我尝试了一些非常直接的方法:我只是把我的 strings.xml\n\t
The complete String looks like this <string name="premium_features_listing_3">- Automatische Aktualisierung der\n\tDatenbank</string>
完整的字符串看起来像这样 <string name="premium_features_listing_3">- Automatische Aktualisierung der\n\tDatenbank</string>
Results in:
结果是:
Automatische Aktualisierung der
Datenbank
自动执行
数据银行
(with no extra line in between)
(中间没有多余的线)
Maybe it will help others. Regards
也许它会帮助别人。问候

