2015-11-29 71 views
0

我正在寫一個XSLT,它除了別的以外,還定位了具有以「_」開頭的值的「name」屬性的某些元素。 。 找到這樣的屬性後,xslt會創建一個xsl:屬性,該名稱是「name」屬性的短劃線之後的所有文本。設置一個屬性的名稱以匹配另一個元素的屬性的子字符串

所以,舉例來說,假設我有以下XML分段:

<someelement> 
    <json:string name="-style">display: block; white-space: pre; border: 2px  
         solid #c77; padding: 0 1em 0 1em; margin: 1em; 
         background-color: #fdd; color: black</json:string> 
        [... some extra elements here ...] 
</someelement> 

而且我希望它成爲

<someelement style="display: block; white-space: pre; border: 2px solid #c77; 
        padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; 
        color: black"> 
       [... some extra elements here ...] 
</someelement> 

Currenty,我想就以下幾個XSLT變化:

<!-- Match string|number|boolean|null elements with a "name" attribute --> 
    <xsl:template match="json:string[@name] | json:number[@name] | json:boolean[@name] | json:null[@name]"> 
    <xsl:choose> 
     <xsl:when test="starts-with(@name,'-')"> 
     <xsl:attribute name="{substring(./[@name],2,string-length(./@name) - 1)}"> 
      <xsl:value-of select="."/> 
     </xsl:attribute> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

,具體而言,這是困擾我行:

<xsl:attribute name="{substring(./[@name],2,string-length(./@name) - 1)}"> 

更具體地說,其中不工作的部分是字符串部分。如果我用一個數字替換整個字符串部分,比如說2,它就可以工作。

是的,我知道substring-after會讓我更好。 我曾嘗試以下,但它不工作之一:

<xsl:attribute name="{substring-after(./[@name],'-')}"> 

我敢肯定,這是某種形式的語法錯誤。

P.S. - 我正在使用XMLSPY進行測試。

非常感謝您的幫助。

+0

您在不當使用方括號。 –

回答

0

我建議你試試這樣說:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*[starts-with(@name, '-')]"> 
    <xsl:attribute name="{substring(@name, 2)}"> 
     <xsl:value-of select="."/> 
    </xsl:attribute>  
</xsl:template> 

</xsl:stylesheet> 

測試:http://xsltransform.net/6r5Gh3g

需要注意的是,如果這些 「特殊」 的元素不是第一次,這將失敗被處理:http://xsltransform.net/6r5Gh3g/1如果這是一種可能性,則還必須匹配父元素並控制處理的順序:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*[starts-with(@name, '-')]"> 
    <xsl:attribute name="{substring(@name, 2)}"> 
     <xsl:value-of select="."/> 
    </xsl:attribute>  
</xsl:template> 

<xsl:template match="*[*[starts-with(@name, '-')]]"> 
    <xsl:copy> 
     <xsl:apply-templates select="*[starts-with(@name, '-')]"/> 
     <xsl:apply-templates select="@*|node()[not(starts-with(@name, '-'))]"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

http://xsltransform.net/6r5Gh3g/3

+0

謝謝,但我確實希望字符串長度在子字符串內運行良好。我怎樣才能做到這一點? :> – dotaneli

+0

@dotaneli沒有必要使用'string-length() '在這裏。請參閱substring()函數的定義:http://www.w3.org/TR/xpath/#function-substring –

+0

謝謝,您現在正確。但是如果我這樣做:,我從XMLSPY得到這個:XSLT 2.0調試錯誤:非法屬性名稱。當前的XML節點是'http://www.google.com'。 – dotaneli

相關問題