如何在由 XSLT 转换为 Excel 工作表的 XML 标记/元素中包含空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3480887/
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
How to include space in XML tag/element which gets transformed by XSLT into Excel sheet
提问by Enggr
I have an XML which gets transformed with an XSLT into an Excel sheet on a webpage. The element tags in XML becomes column headers in Excel. There are columns which would like to have spaces. We don't like underscores or hyphens coming in Excel sheet headers.
How can I incorporate space in an XML tag/element? I tried putting  or %20or #&20;etc. (all kinds of syntax) but all of them shows error on XML editor itself saying this hexadecimal character is not allowed.
我有一个 XML,它通过 XSLT 转换为网页上的 Excel 工作表。XML 中的元素标记成为 Excel 中的列标题。有些列想要有空格。我们不喜欢 Excel 工作表标题中的下划线或连字符。如何在 XML 标签/元素中加入空格?我试图把 或%20或#&20;等(各种语法),但他们都表示对XML编辑器本身说这个十六进制字符错误是不允许的。
My XML is
我的 XML 是
<ClientArray>
<Client>
<LastName>Aanonsen</LastName>
<FirstName>Fred</FirstName>
<Additional Remarks><Additional Remarks>
</Client>
I want a space between Additionaland Remarksin the tag.
Please help. Thanks in advance.
我想要标签之间Additional和Remarks标签中的空格。请帮忙。提前致谢。
回答by Dimitre Novatchev
How to I incorporate space in an XML tag/element? I tried putting or %20 or
etc etc . (all kinds of syntax) but all of them shows error on xml editor itself saying this hexadecimal character is not allowed
如何在 XML 标签/元素中加入空格?我试过把或 %20 或
等等等等。(各种语法)但所有这些都在 xml 编辑器本身上显示错误,说这个十六进制字符是不允许的
You can't. The W3C XML specificationstrictly defines the syntax of names. A name can only start with a letter character (this includes underscore), then the next characters can be letter characters or numbers or the hyphen, but the space is a delimiter and is not allowed as part of any name.
你不能。在W3C XML规范严格定义名称的语法。名称只能以字母字符(包括下划线)开头,接下来的字符可以是字母字符或数字或连字符,但空格是分隔符,不允许作为任何名称的一部分。
More precisely, here are the exact rules from the spec:
[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5] Name ::= NameStartChar (NameChar)*
To overcome this restriction you have to modify your XSLT transformationso that it outputs as Excel column names more convenient to read strings than XML names.
要克服此限制,您必须修改 XSLT 转换,使其输出为 Excel 列名,比 XML 名称更便于读取字符串。
回答by Enggr
Besides Dimitre's exact answer, you could use some pattern like in this stylesheet:
除了 Dimitre 的确切答案之外,您还可以在此样式表中使用一些模式:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="Client/*" name="space-capital">
<xsl:param name="pLetters"
select="translate(name(),'qwertyuiopasdfghjklzxcvbnm','')"/>
<xsl:param name="pString" select="name()"/>
<xsl:param name="pOut" select="''"/>
<xsl:choose>
<xsl:when test="$pString != ''">
<xsl:variable name="vFirst" select="substring($pString,1,1)"/>
<xsl:call-template name="space-capital">
<xsl:with-param name="pLetters" select="$pLetters"/>
<xsl:with-param name="pString"
select="substring($pString,2)"/>
<xsl:with-param name="pOut"
select="concat($pOut,
substring(' ',1,contains($pLetters,
$vFirst)
and
$pOut != ''),
$vFirst
)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($pOut,' : ',.,'
')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With this proper input:
有了这个正确的输入:
<ClientArray>
<Client>
<LastName>Aanonsen</LastName>
<FirstName>Fred</FirstName>
<AdditionalRemarks>Something</AdditionalRemarks>
</Client>
</ClientArray>
Output:
输出:
Last Name : Aanonsen
First Name : Fred
Additional Remarks : Something
In XPath 2.0:
在 XPath 2.0 中:
string-join(/*/*/*/concat(
(: This is the expression you need :)
replace(name(),
'(\P{Lu})(\p{Lu})',
' '),
(: the rest is just to mimic the result :)
' : ',.),
'
')

