2012-11-28 62 views
-1
Sample XSLT: 

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="utf-8"/> 
<xsl:strip-space elements="*"/> 
<xsl:template match="main"> 
<xsl:value-of select="name"/> 
SSN: <xsl:value-of select="ssn"/> 
<xsl:text> 
</xsl:text> 
<xsl:value-of select="address1"/> 
DOB: <xsl:value-of select="dob"/> 
<xsl:text> 
</xsl:text> 
<xsl:value-of select="address2"/> 
GENDER: <xsl:value-of select="gender"/> 
EYE COLOR: <xsl:value-of select="eye"/> 
HEIGHT: <xsl:value-of select="height"/> 
HAIR COLOR: <xsl:value-of select="hair"/> 
WEIGHT: <xsl:value-of select="weight"/> 
</xsl:template> 
</xsl:stylesheet> 


Sample XML: 

    <main> 
<name>CHTP CLS DEBY</name> 
<dob>1999-01-08</dob> 
<ssn>18454512</ssn> 
<address1>115 Z 88TH UL #226-F</address1> 
<address2>ARVADA, CO 80004</address2> 
<gender>M</gender> 
<eye>Blue</eye> 
<hair>Brown</hair> 
<height>182</height> 
<weight>69</weight> 
</main> 

我想要實現這樣的輸出,XSL轉換爲文本

CHTP CLS DEBY        SSN: *****4512 
115 Z 88TH UL #226-F         DOB: 1999-01-08 
ARVADA, CO 80004       

Gender:   M 
Eye Color:  Blue         Height: 182   
Hair Color:  Brown         Weight: 69 

使用XSL轉換。

+0

從什麼轉變? –

+0

你的**輸入**是什麼樣的? [你有什麼**嘗試到目前爲止?](http://www.whathaveyoutried.com)見[Jon Skeet的SO問題清單](http://msmvps.com/blogs/jon_skeet/archive/2012/11 /24/stack-overflow-question-checklist.aspx) - 你已經完成了所有這些嗎? –

+0

輸入是XML文件。我能夠從XML獲取信息,但無法以上述方式顯示它。 –

回答

1

在XSLT 1.0中沒有一個相當於printf("%-30s", something)的通用技巧,我使用的常用技巧是創建一個包含長字符串空格的變量,然後取適當長度的子字符串。

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

    <xsl:variable name="spaces" 
    select="'                 '"/> 

    <xsl:template match="main"> 
    <xsl:call-template name="print-padded"> 
     <xsl:with-param name="str" select="name" /> 
     <xsl:with-param name="width" select="43" /> 
    </xsl:call-template> 
    <xsl:text>SSN: </xsl:text> 
    <xsl:value-of select="ssn" /> 
    <xsl:text>&#x0A;</xsl:text> 

    <xsl:call-template name="print-padded"> 
     <xsl:with-param name="str" select="address1" /> 
     <xsl:with-param name="width" select="53" /> 
    </xsl:call-template> 
    <xsl:text>DOB: </xsl:text> 
    <xsl:value-of select="dob" /> 
    <xsl:text>&#x0A;</xsl:text> 

    <!-- and so on for the other elements --> 
    </xsl:template> 

    <xsl:template name="print-padded"> 
    <xsl:param name="str" select="''" /> 
    <xsl:param name="width" select="0" /> 
    <xsl:value-of select="$str" /> 
    <xsl:value-of select="substring($spaces, 1, $width - string-length($str))" /> 
    </xsl:template> 
</xsl:stylesheet> 

我更喜歡使用的換行字符引用,因爲這是更強大的重新格式化.xsl文件(意外或故意)。

編輯:我注意到你還試圖混淆SSN,您可以使用同樣的伎倆對於

<xsl:variable name="stars" select="'*******************'" /> 
<xsl:value-of select="substring($stars, 1, string-length(ssn) - 4)" /> 
<xsl:value-of select="substring(ssn, string-length(ssn) - 4)" /> 
+0

@ InfantProgrammer'Aravind'能夠以文本格式生成輸出,但我無法執行格式化(即固定間隔選項卡分隔文本)部分。 –

+0

請檢查。我已更新此問題。 –

+0

Comeon !!!我更新了我的問題。請重新打開! –