2014-03-03 78 views
2

我有要求的內容的XML,考慮下面的XML數據 Input1.xml分裂基於XML的任一使用Java或XSLT

<Envelope> 
    <Notification> 
    <Data> 
    <Input>ABCDEFGHIJKLMN</Input> 
    <Output>RESPONSEDATA</Output> 
    </Data> 
    <Data> 
    <Input>OPQRSTUVWXYZ</Input> 
    <Output>NEXTDATA</Output> 
    </Data> 
    <Data> 
    <Input>ALPHABETS</Input> 
    <Output>SOMEDATA</Output> 
    </Data> 
    </Notification> 
    </Envelope> 

現在我想與響應輸出3個XML如下所示文件名以ABCDEFGHIJKLMN的前6個字符作爲輸出文件名如下所示

(FILE1)->ABCDEF.XML 
    <Output>RESPONSEDATA</Output> 
(FILE2)->OPQRST.XML 

    <Output>NEXTDATA</Output> 
(FILE3)->ALPHAB.XML 
<Output>SOMEDATA</Output> 
+0

你使用哪種版本的XSLT的? – Tomalak

+0

我使用XSLT版本「1.0」 – user3367767

回答

2

您使用哪種XSLT 1.0處理器? Xalan的Java支持

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0" 
    xmlns:redirect="http://xml.apache.org/xalan/redirect" 
    extension-element-prefixes="redirect" 
    exclude-result-prefixes="redirect"> 


    <xsl:template match="/Envelope/Notification/Data[not(Input/*)]"> 
    <redirect:write select="concat(substring(Input, 1, 6), '.xml')"> 
     <xsl:copy-of select="Output"/> 
    </redirect:write> 
    </xsl:template> 

    <xsl:template match="/Envelope/Notification/Data[Input/*]"> 
    <redirect:write select="concat(local-name(Input/*), '.xml')"> 
     <xsl:copy-of select="Output"/> 
    </redirect:write> 
    </xsl:template> 

</xsl:stylesheet> 
+0

這不適用於我的eclipse ..我使用eclipse J2eeIndigo是否有任何其他方式使用此代碼? – user3367767

+0

對不起,我不知道你使用了哪個XSLT處理器,我的建議是基於http://xml.apache.org/xalan-j/,基於http://xml.apache.org/xalan-j/extensionslib。 HTML#重定向。 –

+0

您是否建議我將其重定向到其他文件? – user3367767

0

使用在Your Previous Question描述的解決方案,那麼你可以創建3個XSLT文件如下,並應用相同的XML輸入爲每個XSLT /輸出。說明:

<xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='RESPONSEDATA']"/> 

是說,複製所有「/信封/通知/數據」,它的內容,但僅適用於「RESPONSEDATA」的文本值子輸出。

ExtractResponseData.xslt

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output indent="yes" /> 

    <xsl:template match="/"> 
     <xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='RESPONSEDATA']"/> 
    </xsl:template> 
</xsl:stylesheet> 

ExtractNextData.xslt

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output indent="yes" /> 

    <xsl:template match="/"> 
     <xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='NEXTDATA']"/> 
    </xsl:template> 
</xsl:stylesheet> 

ExtractSomeData.xslt

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output indent="yes" /> 

    <xsl:template match="/"> 
     <xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='SOMEDATA']"/> 
    </xsl:template> 
</xsl:stylesheet>