2016-01-26 196 views
-1

我有一个XML其中输入元件将带着100个字符或更多XSLT,创建新节点

输入XML

<?xml version="1.0" encoding="UTF-8" ?> 
<process xmlns="http://xmlns.oracle.com/TEST/TEST_XSLT/BPEL"> 
    <input>abcdefghijlnskdckscksncksckscnkscnkscnksdcnksnc</input> 
</process> 

我的要求,是转变,并为每5个字符的新节点。

<?xml version = '1.0' encoding = 'UTF-8'?> 
<client:processResponse xmlns:client="http://xmlns.oracle.com/TEST/TEST_XSLT/BPEL"> 
    <ns1:Segment-MSG xmlns:ns1="http://schemas.oracle.com/service/bpel/common"> 
     <ns1:Element-127>acfsd</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG xmlns:ns1="http://schemas.oracle.com/service/bpel/common"> 
     <ns1:Element-127>serfg</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG xmlns:ns1="http://schemas.oracle.com/service/bpel/common"> 
     <ns1:Element-127>hjukl</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG xmlns:ns1="http://schemas.oracle.com/service/bpel/common"> 
     <ns1:Element-127>tyuio</ns1:Element-127> 
    </ns1:Segment-MSG> 
. 
. 
. 
. 

</client:processResponse> 

请帮助我完成这个转换。

由于 亚坦

+0

** 1。**输入中的“acfsd”在哪里? - ** 2。**你可以使用XSLT 2.0吗? –

回答

1

假设你是限于XSLT 1.0,尝试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:client="http://xmlns.oracle.com/TEST/TEST_XSLT/BPEL" 
xmlns:ns1="http://schemas.oracle.com/service/bpel/common"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/client:process"> 
    <client:processResponse> 
     <xsl:call-template name="split-string"> 
      <xsl:with-param name="string" select="client:input"/> 
     </xsl:call-template> 
    </client:processResponse> 
</xsl:template> 

<xsl:template name="split-string"> 
    <xsl:param name="string"/> 
    <xsl:param name="length" select="5"/> 
    <ns1:Segment-MSG> 
     <ns1:Element-127> 
      <xsl:value-of select="substring($string, 1, $length)"/> 
     </ns1:Element-127> 
    </ns1:Segment-MSG> 
    <xsl:if test="string-length($string) > $length"> 
     <!-- recursive call --> 
     <xsl:call-template name="split-string"> 
      <xsl:with-param name="string" select="substring($string, $length + 1)"/> 
     </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

结果,当施加到所提供的输入,将是:

<?xml version="1.0" encoding="UTF-8"?> 
<client:processResponse xmlns:client="http://xmlns.oracle.com/TEST/TEST_XSLT/BPEL" xmlns:ns1="http://schemas.oracle.com/service/bpel/common"> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>abcde</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>fghij</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>lnskd</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>cksck</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>sncks</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>ckscn</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>kscnk</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>scnks</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>dcnks</ns1:Element-127> 
    </ns1:Segment-MSG> 
    <ns1:Segment-MSG> 
     <ns1:Element-127>nc</ns1:Element-127> 
    </ns1:Segment-MSG> 
</client:processResponse> 

这是不同于你指定的结果,但我相信是正确的。