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=""[]''"" />
<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>