2015-11-09 152 views
0

我很新的XSL概念,我試圖創建下面的XML格式的XSLT,手動創建XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<row> 
    <c1>1234</c1> 
    <c2>A</c2> 
    <c2 m="1" s="2">321</c2> 
    <c2 m="1" s="3">654</c2> 
    <c2 m="1" s="4">098</c2> 
    <c2 m="2">B</c2> 
    <c2 m="3">C</c2> 
    <c2 m="3" s="2">123</c2> 
    <c2 m="4">5</c2> 
    <c3 /> 
</row> 

如果變換使用XSL那麼我應該得到的輸出如下所示,

1234 A \ 321 \ 654 \ 098] b]ç\ 123] 5

我試圖創建自己如下,

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="row"> 
    <array /> 
    <xsl:apply-templates /> 
    </xsl:template> 
    <xsl:template match="c1"> 
    <data> 
     <xsl:attribute name="attribute">1</xsl:attribute> 
     <xsl:attribute name="value"> 
     <xsl:number level="single" /> 
     </xsl:attribute> 
     <xsl:attribute name="subvalue">1</xsl:attribute> 
     <xsl:value-of select="." /> 
    </data> 
    </xsl:template> 
    <xsl:template match="c2"> 
    <data> 
     <xsl:attribute name="attribute">1</xsl:attribute> 
     <xsl:attribute name="value"> 
     <xsl:number level="single" /> 
     </xsl:attribute> 
     <xsl:attribute name="subvalue">1</xsl:attribute> 
     <xsl:value-of select="." /> 
    </data> 
    </xsl:template> 
</xsl:stylesheet> 

b UT我得到的輸出如下,

1234 A 321 654 098 B C 123 5 

請幫我在創建XSL

+0

提供的XSLT從XML到XML的轉換,而不是XML文本。是否有使用後續變壓器?你在尋找'xsl:text'嗎? – Mitch

+0

@Arunkumar你能解釋一下你用來在輸出中獲得']和\的邏輯嗎? –

+0

@Mitch我沒有使用任何其他變壓器。正如我告訴過你對XSL非常新,所以如果使用xsl:text,我所要求的輸出可以達到,那麼我很高興。 – Arunkumar

回答

0

這是非常混亂。您的XSLT生成<array><data>元素,並具有各種屬性,但在所需輸出中不存在此類元素或屬性。事實上,你的示例代碼似乎與你想要的輸出完全沒有關係。

它總是很難逆向工程從單一實例的要求,但我試圖這樣做會是這樣:

  • 輸出row元素的子元素的字符串值

  • 如果@m1大於先前的@m1,則在字符串值前加上「]」

  • 如果@m1等於先前的@m1,在其前面加上「\」

  • 如果沒有@m1,則在其前面加一個空格。

如果我的猜測是任何接近的標誌,那麼解決方案將是這個樣子:

<xsl:template match="row"> 
    <xsl:apply-templates select="*"/> 
</xsl:template> 

<xsl:template match="row/*[@m1 > preceding-sibling::*[1]/@m1]"> 
    <xsl:value-of select="concat(']', .)"/> 
</xsl:template> 

<xsl:template match="row/*[@m1 = preceding-sibling::*[1]/@m1]"> 
    <xsl:value-of select="concat('\', .)"/> 
</xsl:template> 

<xsl:template match="row/*[not(@m1)]"> 
    <xsl:value-of select="concat(' ', .)"/> 
</xsl:template>