2013-10-17 77 views
1

我必须使用XSL 1.0来解决以下问题。我是XSL的初学者,所以请耐心等待,如果它是微不足道的。XSL 1.0难度拆分字符串

我使用名为transform.xsl的文件将left.xml转换为right.xml。我将显示这些文件,然后解释逻辑。

这里是left.xml:

<?xml version="1.0" ?> 
<parties> 
    <party> 
     <id>123</id> 
     <pending>456,789,234</pending> 
     <name>NYC Film Festival</name> 
    </party> 

    <party> 
     <id>345</id> 
     <pending>234</pending> 
     <name>Montreal Film Festival</name> 
    </party> 

    <party> 
     <id>345</id> 
     <pending /> 
     <name>LA Film Festival</name> 
    </party> 
</parties> 

这里是right.xml

<?xml version="1.0" ?> 
    <parties> 
     <Aparty name="NYC Film Festival" id="456"/> 
     <Aparty name="NYC Film Festival" id="789"/> 
     <Aparty name="NYC Film Festival" id="234"/> 

     <Aparty name="Montreal Film Festival" id=234/> 

     <Aparty name="LA Film festival" id=345> 

    </parties> 

的思路如下。考虑一下left.xml。如果派对内的挂起节点为空,则将id用于right.xml。否则,将待处理标记内的内容拆分并为每个标记插入一个节点。分裂正在抛弃我,尤其是因为我必须使用XSL 1.0

有人可以帮忙吗?

我现在transform.xsl看起来像这样

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

    <xsl:template match="/"> 
     <xsl:element name="parties"> 
      <xsl:apply-templates select="parties/party"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="parties/party"> 
     <xsl:choose> 
      <xsl:when test="boolean(pending) and string(pending) != ''"> 
       <xsl:element name="Aparty"> 
        <xsl:attribute name="name"> 
         <xsl:value-of select="name"/> 
        </xsl:attribute> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="pending"/> 
        </xsl:attribute> 
       </xsl:element> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:element name="Aparty"> 
        <xsl:attribute name="name"> 
         <xsl:value-of select="name"/> 
        </xsl:attribute> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="id"/> 
        </xsl:attribute> 
       </xsl:element> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

回答

0

您可以处理另一模板递归挂起值的列表。这个想法是为第一个待处理的id生成Aparty,然后传递剩下的ids用于下一次迭代处理。

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

    <xsl:template match="/"> 
    <xsl:element name="parties"> 
     <xsl:apply-templates select="parties/party"/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="parties/party"> 
    <xsl:choose> 
     <xsl:when test="boolean(pending) and string(pending) != ''"> 
     <xsl:call-template name="generateAparty"> 
      <xsl:with-param name="name" select="name"/> 
      <xsl:with-param name="ids" select="pending"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:element name="Aparty"> 
      <xsl:attribute name="name"> 
      <xsl:value-of select="name"/> 
      </xsl:attribute> 
      <xsl:attribute name="id"> 
      <xsl:value-of select="id"/> 
      </xsl:attribute> 
     </xsl:element> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template name="generateAparty"> 
    <xsl:param name="name"/> 
    <xsl:param name="ids"/> 

    <xsl:choose> 
     <xsl:when test="contains($ids, ',') = false()"> 
     <!-- 1 id left --> 
     <xsl:element name="Aparty"> 
      <xsl:attribute name="name"> 
      <xsl:value-of select="$name"/> 
      </xsl:attribute> 
      <xsl:attribute name="id"> 
      <xsl:value-of select="$ids"/> 
      </xsl:attribute> 
     </xsl:element> 
     </xsl:when> 
     <xsl:otherwise> 
     <!-- Create element for 1st pending id in the list --> 
     <xsl:element name="Aparty"> 
      <xsl:attribute name="name"> 
      <xsl:value-of select="$name"/> 
      </xsl:attribute> 
      <xsl:attribute name="id"> 
      <xsl:value-of select="substring-before($ids, ',')"/> 
      </xsl:attribute> 
     </xsl:element> 
     <xsl:call-template name="generateAparty"> 
      <xsl:with-param name="name" select="$name"/> 
      <xsl:with-param name="ids" select="substring-after($ids, ',')"/> 
     </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet>