1
votes

I have to use XSL 1.0 to solve the following problem. I am a beginner to XSL, so please bear with me if it is trivial.

I use a file called transform.xsl to transform left.xml to right.xml. I will show the files, then explain the logic.

Here is 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>

Here is 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>

The idea is as follows. Consider the left.xml. If the pending node inside party is empty, use id for right.xml. Otherwise, split the content inside the pending tag and insert a node for each. The splitting is throwing me off, especially since I have to use XSL 1.0

Can someone please help?

My current transform.xsl looks like this

<?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>
1

1 Answers

0
votes

You can process the list of pending values recursively in another template. The idea is to generate Aparty for the first pending id, then pass the remaining ids for next iteration to handle.

<?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>