3

XSLを使用して解析および表示したい画像のURLを含むリストがありますが、最初の3つの画像のみが必要です。

例えば

<xsl:value-of select="@Icons" />

戻り値:

['http://www.test.com/image1.jpg',
 'http://www.test.com/image2.jpg',
 'http://www.test.com/image3.jpg',
 'http://www.test.com/image4.jpg',
 'http://www.test.com/image5.jpg']

最終的な出力は次のようになります。

<img src="http://www.test.com/image1.jpg" alt=""/>
<img src="http://www.test.com/image2.jpg" alt=""/>
<img src="http://www.test.com/image3.jpg" alt=""/>

最初の3つの項目を選択するコードの一部がありますが、リストを解析してHTMLで画像を表示する方法がわかりません。

<xsl:template name="recurse_till_ten">
    <xsl:param name="num">1</xsl:param>
    <xsl:if test="not($num = 3)">
        //Parasing list here
        <xsl:call-template name="recurse_till_ten">
            <xsl:with-param name="num">
                <xsl:value-of select="$num + 1">
            </xsl:with-param>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
4

3 に答える 3

1

はるかに短い XSLT 2.0 ソリューション:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vDelims">\s*'\s*,?</xsl:variable>
 <xsl:variable name="vTokens" select="tokenize(/*/@Icons, $vDelims)[.]"/>

 <xsl:template match="/*">
  <xsl:for-each select="$vTokens[position() le 3]">
   <img src="{.}" alt=""/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

この変換が次の XML ドキュメントに適用される場合:

<t Icons=
 "'http://www.test.com/image1.jpg',
  'http://www.test.com/image2.jpg',
  'http://www.test.com/image3.jpg',
  'http://www.test.com/image4.jpg',
  'http://www.test.com/image5.jpg'"/>

必要な正しい結果が生成されます。

<img src="http://www.test.com/image1.jpg" alt=""/>
<img src="http://www.test.com/image2.jpg" alt=""/>
<img src="http://www.test.com/image3.jpg" alt=""/>
于 2012-09-06T04:57:54.797 に答える
1

XSLT 1.0 ソリューション

この XSLT 1.0 スタイルシート...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
 <xsl:variable name="strip-chars" select="&quot;[]''&quot;" /> 
 <xsl:for-each select="*">
   <xsl:copy>
     <xsl:call-template name="tokenize">
       <xsl:with-param name="content" select="concat(translate(@Icons,$strip-chars,''),',')" />
       <xsl:with-param name="count" select="3" />
     </xsl:call-template>
   </xsl:copy>
  </xsl:for-each>
</xsl:template>

<xsl:template name="tokenize">
  <xsl:param name="content" />
  <xsl:param name="count" />
  <xsl:if test="contains($content,',') and $count > 0">
    <img src="{substring-before($content,',')}" alt="" />
    <xsl:call-template name="tokenize">
      <xsl:with-param name="content" select="normalize-space(substring-after($content,','))" />
      <xsl:with-param name="count" select="$count - 1" />
    </xsl:call-template>
  </xsl:if>  
</xsl:template>

</xsl:stylesheet>

...この入力ドキュメントに適用すると...

<t Icons="['http://www.test.com/image1.jpg',
 'http://www.test.com/image2.jpg',
 'http://www.test.com/image3.jpg',
 'http://www.test.com/image4.jpg',
 'http://www.test.com/image5.jpg']"/>

...譲ります...

<t>
  <img src="http://www.test.com/image1.jpg" alt="" />
  <img src="http://www.test.com/image2.jpg" alt="" />
  <img src="http://www.test.com/image3.jpg" alt="" />
</t>

そして、この短い入力ドキュメントに適用すると...

<t Icons="['http://www.test.com/image1.jpg',
 'http://www.test.com/image2.jpg']"/>

...収量...

<t>
  <img src="http://www.test.com/image1.jpg" alt="" />
  <img src="http://www.test.com/image2.jpg" alt="" />
</t>

ノート

編集時のSureshのソリューションは、画像の数が3以下であるこの2番目のユースケースでは機能しません。


XSLT 2.0 ソリューション

XSLT 2.0 では、このようなスタイルシートを使用して、同じ結果をより簡単に、より効率的に、より拡張的に達成できます...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
 <xsl:for-each select="*">
   <xsl:copy>
    <xsl:variable name="image-elements" as="element()*">
      <xsl:analyze-string select="@Icons" regex="'([^']*)'(,|\])" >
       <xsl:matching-substring>
         <img src="{regex-group(1)}" alt="" />
       </xsl:matching-substring>
      </xsl:analyze-string>
    </xsl:variable>
    <xsl:copy-of select="$image-elements[not(position() > 3)]" />
   </xsl:copy>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

XSLT 2.0 XPATH ソリューション

参考までに、純粋に XPATH を使用した XSLT 2.0 のバリエーションを次に示します。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
 <xsl:for-each select="*">
   <xsl:copy>
    <xsl:for-each select="(for $i in tokenize(@Icons,',') return
                           replace($i,'.*''([^'']*)''.*','$1'))
                            [not(position() > 3)]">
      <img src="{.}" alt="" />
    </xsl:for-each>
   </xsl:copy>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Dimitre のソリューションの更新とコメント

より簡潔な XSLT 2.0 ソリューションについて Dimitre にお祝いを申し上げます。彼は、角括弧を含まない私のものとは少し異なる入力を使用しています。Dimitre のソリューションを見ると、さらに大幅に小さくするための微調整が見られます。

Dimitre のソリューションを微調整したこの XSLT 2.0 スタイルシートは、すべてのソリューションの中で最も厳密なものであり、おまけとして、私の形式の入力ドキュメントと彼の形式の両方で動作し、同じ正しい結果ドキュメントを生成します。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
  <xsl:for-each select="tokenize(/*/@Icons, '\[?\s*''\s*,?\]?')[.][position() le 3]">
   <img src="{.}" alt=""/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
于 2012-09-06T01:03:06.290 に答える
0

トークン化関数を使用します。トークン化に関するすばらしい記事がhttp://www.xml.com/pub/a/2003/05/07/tr.htmlにあります。(または)

これを使って

<?xml version="1.0" standalone="yes"?> 
<xsl:stylesheet
 version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
   <xsl:variable name="v" select="data/text()"/>
      <list>

      <xsl:call-template name="parse">
        <xsl:with-param name="in" select="normalize-space(translate($v,'[]',''))"/>
        <xsl:with-param name="num">0</xsl:with-param>
      </xsl:call-template>  
  </list> 
</xsl:template>

<xsl:template name="parse">
    <xsl:param name="in"/>
    <xsl:param name="num"/>
    <xsl:variable name="char">'</xsl:variable>
    <xsl:if test="$num &lt; 3">
        <xsl:element name="image">
           <xsl:attribute name="href"><xsl:value-of select="translate(substring-before($in,','),$char,'')"/></xsl:attribute>
        </xsl:element>
      <xsl:call-template name="parse">
        <xsl:with-param name="in" select="substring-after($in,',')"/>
        <xsl:with-param name="num"><xsl:value-of select="$num + 1"/></xsl:with-param>
      </xsl:call-template>  
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
于 2012-09-05T20:17:22.940 に答える