2010-08-13 28 views
9

我有一個XML,它可以通過XSLT轉換爲網頁上的Excel工作表。 XML中的元素標記成爲Excel中的列標題。有列想要有空格。我們不喜歡Excel工作表頭中的下劃線或連字符。 如何將空間合併到XML標記/元素中?我試過把 %20#&20;等(各種語法),但他們都顯示XML編輯器本身的錯誤,說這個十六進制字符是不允許的。如何在由XSLT轉換爲XML表格的XML標記/元素中包含空格

我的XML是

<ClientArray> 
    <Client> 
    <LastName>Aanonsen</LastName> 
    <FirstName>Fred</FirstName> 
    <Additional Remarks><Additional Remarks> 
    </Client> 

我想在標籤AdditionalRemarks之間的空間。 請幫忙。提前致謝。

+0

好問題(+1)。詳細解釋爲什麼這是不可能的,請參閱我的答案。 – 2010-08-13 22:11:11

回答

15

如何在XML中嵌入空間 tag/element?我嘗試過放置或%20或 &#20;等等。 (各種語法),但他們都表示對XML 編輯器本身說這個十六進制字符 錯誤是不允許

你不能W3C XML specification嚴格定義了名稱的語法。一個名字只能以一個字母開頭(這包括下劃線,那麼下一個字符可以是字母字符或數字或連字符,但空格是分隔符,並且不允許作爲任何名字的一部分)

更多正是,這裏有the exact rules from the spec

[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040] 
[5] Name ::= NameStartChar (NameChar)* 

爲了克服這個限制,你必須修改你的XSLT轉換,使其輸出爲的Excell列名更方便閱讀的字符串比XML名稱

+0

Upvoted for Backus-Naur;相當有幫助,謝謝。 – Dan 2014-07-23 20:01:48

3

除了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,' : ',.,'&#xA;')"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

有了這個正確的輸入:

<ClientArray> 
    <Client> 
    <LastName>Aanonsen</LastName> 
    <FirstName>Fred</FirstName> 
    <AdditionalRemarks>Something</AdditionalRemarks> 
    </Client> 
</ClientArray> 

輸出:

Last Name : Aanonsen 
First Name : Fred 
Additional Remarks : Something 

在XPath 2.0:

string-join(/*/*/*/concat(
       (: This is the expression you need :) 
        replace(name(), 
          '(\P{Lu})(\p{Lu})', 
          '$1 $2'), 
       (: the rest is just to mimic the result :) 
        ' : ',.), 
      '&#xA;') 
+0

+1對於良好的邏輯 – miti737 2010-08-18 02:47:31