2

コンテンツをxslでラップする方法を探しています。これは私がしていることの単純化された例です。多くのコンテンツ...はかなりの量のコンテンツであり、アンカータグは例としてのみ使用されます。それはdivか何か他のものである可能性があります。

XML:

<root>
    <attribution>John Smith</attribution>
    <attributionUrl>http://www.johnsmith.com</attributionUrl>
</root>

XSL:私が現在どのようにやっているのか。これはかなりの量のxslを追加しており、単純化する方法があると確信しています。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <xsl:if test="attribution != ''">
        <xsl:choose>
            <xsl:when test="attributionUrl != ''">
                <a>
                    <xsl:attribute name="href"><xsl:value-of select="attributionUrl"/></xsl:attribute>
                    <span>Thank you, <xsl:value-of select="attribution"/></span>
                    <div>Lots of content ...</div>
                </a>
            </xsl:when>
            <xsl:otherwise>
                <span>Thank you, <xsl:value-of select="attribution"/></span>
                    <div>Lots of content ...</div>
            </xsl:otherwise>
        </xsl:choose>   
    </xsl:if>
</xsl:template>

XSL:概念的には、これが私がやりたいことです。無効なXMLであるため機能しませんが、アイデアをキャプチャします。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <xsl:if test="attributionUrl != ''">
        <a>
    </xsl:if>

    <xsl:attribute name="href"><xsl:value-of select="attributionUrl"/></xsl:attribute>
    <span>Thank you, <xsl:value-of select="attribution"/></span>
    <div>Lots of content ...</div>

    <xsl:if test="attributionUrl != ''">
        </a>
    </xsl:if>
</xsl:template>

編集:

私はの複数のバージョンを避けようとしています<div>Lots of content ...</div>

4

2 に答える 2

1

「ありがとう」を実行するテンプレートを作成して、両方の場合に使用できます。<xsl:if>とにかく、またはの代わりにテンプレートマッチングを使用することは<xsl:when>、XMLの精神に基づいています。

<xsl:template match="root[attributionUrl!='']">
  <a href="{attributionUrl}">
    <xsl:call-template name="thankYou"/>
  </a>
</xsl:template>

<xsl:template match="root" name="thankYou">
  <span>Thank you, <xsl:value-of select="attribution"/></span>
  <div>Lots of content ...</div>
</xsl:template>
于 2013-01-09T22:12:19.610 に答える
1

これはそれを行う必要があります:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="text()" />

  <xsl:template match="root[attributionUrl !='' and attribution != '']">
    <a href="{attributionUrl}">
      <xsl:apply-templates select="attribution" />
    </a>
  </xsl:template>

  <xsl:template match="attribution[. != '']">
    <span>
      Thank you, <xsl:value-of select="attribution"/>
    </span>
    <div>Lots of content ...</div>
  </xsl:template>
</xsl:stylesheet>
于 2013-01-10T00:07:30.580 に答える