2013-04-02 73 views
0

如何使用分隔元素;作为分隔符。我的要求如下所示。拆分属性使用;作为XSLT中的分隔符

输入:

<Element1>C:KEK39519US; U:085896395195; A:K39519US; B:S2345843</Element1> 

输出:

<CustItem>KEK39519US</CustItem> 

<UNumber>085896395195</UNumber> 

<ANumber>K39519US</ANumber> 

<BNumber>S2345843</BNumber> 

输入是每一个不same.some谈到几次都是C:KEK39519US; U:085896395195; B:S2345843 一段时间,这样C:KEK39519US; A:K39519US; B:S2345843 有时像这样U:085896395195; A:K39519US; 一段时间一样这C:KEK39519US; U:085896395195; A:K39519US;

+0

您使用的是XSLT 1.0还是2.0? –

+0

我正在使用XSLT 1.0 – sum

回答

2

要在XSLT 1.0中解决这个问题,您可能需要一个递归调用自己的命名模板。该模板将在第一个分号之前处理该字符串,并相应地输出该元素。然后它将递归调用本身带有字符串的剩余部分这分号之后(如果有的话)

以下是完整的XSLT

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

    <xsl:template match="Element1"> 
     <xsl:call-template name="outputElements"> 
     <xsl:with-param name="list" select="." /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="outputElements"> 
     <xsl:param name="list"/> 

     <xsl:variable name="first" select="normalize-space(substring-before(concat($list, ';'), ';'))"/> 
     <xsl:variable name="remaining" select="normalize-space(substring-after($list, ';'))"/> 

     <xsl:call-template name="createElement"> 
     <xsl:with-param name="element" select="$first" /> 
     </xsl:call-template> 

     <!-- If there are still elements left in the list, call the template recursively --> 
     <xsl:if test="$remaining"> 
     <xsl:call-template name="outputElements"> 
      <xsl:with-param name="list" select="$remaining"/> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template name="createElement"> 
     <xsl:param name="element"/> 
     <xsl:variable name="elementName"> 
     <xsl:choose> 
      <xsl:when test="substring-before($element, ':') = 'C'">CustItem</xsl:when> 
      <xsl:otherwise><xsl:value-of select="concat(substring-before($element, ':'), 'Number')" /></xsl:otherwise> 
     </xsl:choose> 
     </xsl:variable> 
     <xsl:element name="{$elementName}"> 
     <xsl:value-of select="substring-after($element, ':')" /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

当适用于您的XML,下面是输出

<CustItem>KEK39519US</CustItem> 
<UNumber>085896395195</UNumber> 
<ANumber>K39519US</ANumber> 
<BNumber>S2345843</BNumber> 

请注意使用属性值模板指定每个新元素的名称。