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>

ここに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 について考えてみましょう。パーティ内の保留中のノードが空の場合は、right.xml に id を使用します。それ以外の場合は、保留中のタグ内でコンテンツを分割し、それぞれにノードを挿入します。特に 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 に答える