【问题标题】:XSL 1.0 difficulty splitting stringXSL 1.0 难以拆分字符串
【发布时间】:2013-10-17 01:00:34
【问题描述】:

我必须使用 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>

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    您可以在另一个模板中递归处理待处理值列表。这个想法是为第一个待处理的 id 生成Aparty,然后将剩余的 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>
    

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 2014-06-29
      相关资源
      最近更新 更多