2012-01-12 64 views
1

對於熟悉xslt的人的問題。不久之後 - 我需要通過一次轉換來擴展屬性。有誰知道該怎麼做?例如,我有XML擴展的結果xml節點屬性

<article type="funny"> 
    <firstclass>big</firstclass> 
    <secondclass>smooth</secondclass> 
    hot-dog 
</article> 

,我需要有輸出:

<div class="funny big smooth">hot-dog</div> 

可以將它與每個標籤或屬性(的Firstclass,二等和類型)單獨的模板來完成?

當然,我可以輸入這樣的事情:

<xsl:attribute> 
    <xsl:value-of select="@type"/>&#160; 
    <xsl:value-of select="secondclass"/>&#160; 
    <xsl:value-of select="firstclass"/> 
</xsl:attribute> 

,但有很多可以將它置於與否,可以擴展類或沒有屬性的,所以這將是完美的可能是這樣的

<xsl:apply-templates select="@* | node()"/> 

爲處理所有他們硬編碼。

謝謝!

+0

,當然我可以通過應用兩個xslt - 一個逐個製作假類標籤,另一個來指導他們。但是它應該在一個xslt =( – 2012-01-12 13:11:30

回答

0

這種轉變

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

<xsl:template match="article"> 
    <div> 
     <xsl:attribute name="class"> 
     <xsl:apply-templates select="@type | *"/> 
     </xsl:attribute> 

     <xsl:apply-templates select="text()"/> 
    </div> 
</xsl:template> 

<xsl:template match="article/*"> 
    <xsl:if test="not(position()=1)"><xsl:text> </xsl:text></xsl:if> 
    <xsl:value-of select="."/> 
</xsl:template> 

<xsl:template match="article/text()"> 
    <xsl:value-of select="normalize-space()"/> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<article type="funny"> 
    <firstclass>big</firstclass> 
    <secondclass>smooth</secondclass> 
    hot-dog 
</article> 

產生想要的,正確的結果

<div class="funny big smooth">hot-dog</div> 
+0

)中完成所以元素(@type | *)只會擴展類attr,text()會擴展結果體,但是我想要省略硬編碼this,因爲我不' t知道這些節點會擴展導致的div類,並且會擴展body。 – 2012-01-13 11:08:54

+0

@DaniilGrudzinskiy:請問您是否重新說明這一點?我不明白您在說什麼... – 2012-01-13 13:40:36