2013-10-17 124 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>