2016-07-02 28 views
0

我是新來的XSLT,下面是XML輸入XSLT:XML到文本的轉換

<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ser="http://xyz.com.zr/l8q/12Q/service/"> 
    <soapenv:Header> 
     <ser:User> 
      <!-- comment --> 
      <Username/> 
      <password/> 
     </ser:User> 
    </soapenv:Header> 
    <soapenv:Body> 
     <mainTag> 
      <abc>1596056</abc> 
      <asdd>12434F</asdd> 
      <childtag> 
       <asdf>1233</asdf> 
       <qwe>567</qwe> 
      </childtag> 
     </mainTag> 
    </soapenv:Body> 
</soapenv:Envelope> 
以下

時所需的輸出(文本)需要

01|1596056|12434F 
02|1233| |567| 
在總結

所以,要實現期望的輸出,在以下知道位數的細節

1) how can i make the text output from xslt. 
2) how to make/avoid newline (i.e break). 
3) how to generate spaces in text. 
下面

是邏輯

01  = this is line number in the text (first line) 
1596056 = <abc> 
12434F = <asdd> 

02  = this is line number in the text (second line) 
1233 = <asdf> 
567  = <qwe> 

感謝

+0

開始使用'' – Pawel

+0

該示例不明確。請解釋這裏需要應用的邏輯。 –

+0

我仍然不知道你的例子中的內容是不變的,這只是一個例子。 –

回答

0

你的邏輯是有些不一致,但是我不會糾纏於這一點。相反,我寫了一個xsl來產生輸出,並回答你的目標1)2)和3)。我在相關代碼點嵌入了評論。

這些是xslt的基本概念,所以如果你不能遵循它,我建議做一些關於xslt的背景知識。有很多資源爲此.. http://www.w3schools.com彈出想法,但當然會有更多的網絡上。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 

<!-- 1) output method is "text" --> 
<xsl:output method="text" version="1.0" encoding="UTF-8"/> 

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

<!-- matches on soap envolpe body Body for the namespace http://schemas.xmlsoap.org/soap/envelope/ --> 
<xsl:template match="s:Body"> 
    <!-- list of tags that you want to count --> 
    <xsl:apply-templates select="mainTag | mainTag/childtag "/> 
</xsl:template> 

<xsl:template match="mainTag"> 
    <!-- output context position from within nodeset specified with the apply-templates above--> 
    <xsl:value-of select="concat('0',position())"/> 
    <xsl:text>|</xsl:text> 
    <xsl:value-of select="abc"/> 
    <xsl:text>|</xsl:text> 
    <xsl:value-of select="asdd"/> 
    <!-- 2) new line using entity --> 
    <xsl:text>&#13;</xsl:text> 
</xsl:template> 

<xsl:template match="childtag"> 
    <xsl:value-of select="concat('0',position())"/> 
    <xsl:text>|</xsl:text> 
    <xsl:value-of select="asdf"/> 
    <!-- 3) you can include a space within the xsl:text --> 
    <xsl:text>| |</xsl:text> 
    <xsl:value-of select="qwe"/> 
    <xsl:text>|</xsl:text> 
    <!-- 2) new line using entity --> 
    <xsl:text>&#13;</xsl:text> 
</xsl:template> 

</xsl:stylesheet>