2011-06-28 39 views
0

我需要使用XSLT將以下XML轉換爲帶有分隔符「|」的平面文件。使用XSLT將XML轉換爲平面文件

這是我的XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><GeneralLedgerReport targetNamespace="http://www.portal.com/schemas/GLSync" xmlns="http://www.portal.com/schemas/GLSync" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.portal.com/schemas/GLSync brm_gl_data.xsd"> 
     <SourceSystemID>Germany</SourceSystemID> 
     <ReportID>0.0.0.1-697031-1136</ReportID> 
     <RevenueType>Unbilled earned</RevenueType> 
     <BRM_GL_Segment>.</BRM_GL_Segment> 
     <ReportCreatedTime> 
       <Year>2013</Year> 
       <Month>01</Month> 
       <Day>14</Day> 
       <Hours>16</Hours> 
       <Minutes>2</Minutes> 
       <Seconds>3</Seconds> 
     </ReportCreatedTime> 
     <PeriodStartTime> 
       <Year>2012</Year> 
       <Month>10</Month> 
       <Day>15</Day> 
       <Hours>0</Hours> 
       <Minutes>0</Minutes> 
       <Seconds>0</Seconds> 
     </PeriodStartTime> 
     <PeriodEndTime> 
       <Year>2012</Year> 
       <Month>10</Month> 
       <Day>16</Day> 
       <Hours>0</Hours> 
       <Minutes>0</Minutes> 
       <Seconds>0</Seconds> 
     </PeriodEndTime> 
</GeneralLedgerReport> 

所需的輸出: 總帳報表

德國| 0.0.0.1-697031-1136 |未開票賺| 2013/01/14-16:02: 03 | 2012/10/15-00:00:00 德國| 0.0.0.1-697031-1136 | Unbilled賺取| 2013/01/14-16:02:03 | 2012/10/15-00:00:00 德國| 0.0.0.1-697031-1136 | Unbilled賺得| 2013/01/14-16:02:03 | 2012/10/15-00:00:00 德國| 0.0.0.1-697031-1136 | | 2013/01/14-16:02:03 | 2012/10/15-00:00:00

我試過幾件事情,但輸出當屬:

Germany0.0.0.1-697031-1136Unbilled earned20130114160203201210150

需要此緊急,請大家幫助! 謝謝!

我是新來的XSLT,不知道如何用分隔符分開得到的元素,我想這一點:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" 
     xmlns:java="http://xml.apache.org/xalan/java" exclude-result-prefixes="exslt java"> 
<xsl:output method="text" version="1.0" encoding="ISO-8859-1" indent="yes"/> 

     <xsl:template match="/"> 
      <xsl:apply-templates select="/"/> 
     </xsl:template> 
     <xsl:template match="/"> 
      <xsl:value-of select="."/> 
     </xsl:template> 

</xsl:stylesheet> 

更多XML:

<SourceSystemID>Germany</SourceSystemID> 
    <ReportID>0.0.0.1-697031-1116</ReportID> 
    <RevenueType>Unbilled earned</RevenueType> 
    <BRM_GL_Segment>.</BRM_GL_Segment> 
    <ReportCreatedTime> 
      <Year>2013</Year> 
      <Month>01</Month> 
      <Day>14</Day> 
      <Hours>16</Hours> 
      <Minutes>1</Minutes> 
      <Seconds>59</Seconds> 
    </ReportCreatedTime> 
    <PeriodStartTime> 
      <Year>2012</Year> 
      <Month>09</Month> 
      <Day>25</Day> 
      <Hours>0</Hours> 
      <Minutes>0</Minutes> 
      <Seconds>0</Seconds> 
    </PeriodStartTime> 
    <PeriodEndTime> 
      <Year>2012</Year> 
      <Month>09</Month> 
      <Day>26</Day> 
      <Hours>0</Hours> 
      <Minutes>0</Minutes> 
      <Seconds>0</Seconds> 
    </PeriodEndTime> 
    <SourceSystemID>Germany</SourceSystemID> 
    <ReportID>0.0.0.1-697031-1136</ReportID> 
    <RevenueType>Unbilled earned</RevenueType> 
    <BRM_GL_Segment>.</BRM_GL_Segment> 
    <ReportCreatedTime> 
      <Year>2013</Year> 
      <Month>01</Month> 
      <Day>14</Day> 
      <Hours>16</Hours> 
      <Minutes>2</Minutes> 
      <Seconds>3</Seconds> 
    </ReportCreatedTime> 
    <PeriodStartTime> 
      <Year>2012</Year> 
      <Month>10</Month> 
      <Day>15</Day> 
      <Hours>0</Hours> 
      <Minutes>0</Minutes> 
      <Seconds>0</Seconds> 
    </PeriodStartTime> 
    <PeriodEndTime> 
      <Year>2012</Year> 
      <Month>10</Month> 
      <Day>16</Day> 
      <Hours>0</Hours> 
      <Minutes>0</Minutes> 
      <Seconds>0</Seconds> 
    </PeriodEndTime> 
+0

您必須聲明命名空間(前綴和URI),否則您將無法在輸入樹中選擇元素。而且,你想要的輸出是否正確?這只是一個重複四次? –

+0

@empo:基本上輸入是一個巨大的XML以相同的方式重複,這就是爲什麼我需要輸出爲相同數量的行重複。我會嘗試添加前綴和URI。它是否正確? <?xml version =「1.0」encoding =「UTF-8」?> Gagan

+0

請發佈多一點您的輸入xml(編輯您的問題請勿使用評論) –

回答

1

試着看一下這個問題,但使用|而不是逗號

XSLT Concatenating the values with comma (,)

+0

謝謝康拉德!我是XSLT的新手,不確定如何分別挑選每個元素。我只是嘗試了以下內容:<?xml version =「1.0」encoding =「UTF-8」?>< xsl:template match =「/」>可以請你幫忙,我試着尋找一些例子, – Gagan

+0

伊格納西奧,請參閱上面的帖子,我已經發布了代碼嘗試 – Gagan

+0

我不讀那個。編輯你的問題。 –

0

你可以從下面的XSLT一些啓示:

<?xml version='1.0' encoding='utf-8'?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:glsync="http://www.portal.com/schemas/GLSync" version="1.0"> 

    <xsl:output method="text" /> 

    <xsl:template match="glsync:GeneralLedgerReport"> 
     <xsl:value-of select="./glsync:SourceSystemID" /> 
     <xsl:text>|</xsl:text> 
     <xsl:value-of select="./glsync:ReportID" /> 
     <xsl:text>|</xsl:text> 
     <xsl:value-of select="./glsync:RevenueType" /> 
     <xsl:text>|</xsl:text> 
     <xsl:apply-templates select="./glsync:ReportCreatedTime" /> 
     <xsl:text>|</xsl:text> 
     <xsl:apply-templates select="./glsync:PeriodStartTime" /> 
    </xsl:template> 

    <xsl:template match="glsync:ReportCreatedTime"> 
     <xsl:value-of 
      select="concat(
       glsync:Year, '/', 
       glsync:Month, '/', 
       glsync:Day, '-', 
       format-number(glsync:Hours, '00'), ':', 
       format-number(glsync:Minutes, '00'), ':', 
       format-number(glsync:Seconds, '00'))" /> 
    </xsl:template> 

    <xsl:template match="glsync:PeriodStartTime"> 
     <xsl:value-of 
      select="concat(
       glsync:Year, '/', 
       glsync:Month, '/', 
       glsync:Day, '-', 
       format-number(glsync:Hours, '00'), ':', 
       format-number(glsync:Minutes, '00'), ':', 
       format-number(glsync:Seconds, '00'))" /> 
    </xsl:template> 

</xsl:stylesheet> 

一些注意事項:

  • 你必須匹配的元素使用的命名空間,因爲你的XML有一個targetNamespace;

  • 你可以使用concat功能也匹配glsync:GeneralLedgerReport

  • 注意使用format-number函數來獲取小時,分鐘和秒兩位數

  • 終於模板,您不再XML似乎奇怪,因爲所有GeneralLedgerReport,ReportCreatedTime,PeriodStartTimePeriodEndTime標籤是在同一水平;也許你有一些標籤封裝,從而使您有嵌套結構,每GeneralLedgerReport

我希望這有助於。