1

我必须使用 XSL 1.0 来解决以下问题。我是 XSL 的初学者,所以如果它是微不足道的,请多多包涵。

我使用一个名为 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>

这里是对的.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>
4

1 回答 1

0

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

<?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>
于 2013-10-17T02:26:05.067 回答