aqubi+shin1

Recent site activity

xsl‎ > ‎

XSLT


XMLとXSL

sample.xml
<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="sample.xml" ?>

<feeds>
  <feed name="aaa">bbbbbb</feed>
  <feed name="ccc">dddddd</feed>
</feeds>

sample.xsl
<?xml version="1.0" encoding="Shift_JIS" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />

<xsl:template match="/">
    <html>
    <body>
    <xsl:for-each select="feeds/feed">
        <xsl:value-of select="@name" /> : <xsl:value-of select="." />
    </xsl:for-each>
    </body>
    </html>
</xsl:template>


xsl要素

外部XSLファイルの読み込み

xsl:import
xsl:apply-imports
xsl:include

ノード/属性の作成

xsl:element
xsl:attribute
xsl:attribute-set
xsl:comment
xsl:text

ノードのコピー

xsl:copy
xsl:copy-of

Templateの呼び出し

xsl:call-template
xsl:apply-template
xsl:with-param

値の出力

xsl:value-of

条件

xsl:if
xsl:choose
  xsl:when
  xsl:otherwise

繰り返し処理

xsl:for-each

空白文字の処理

xsl:preserve-space
xsl:strip-space

フォーマット

xsl:number
xsl:decimal-format

非対応の要素の代替

xsl:fallback


Tips

xsl:outputの指定方法

XMLを出力するとき
<xsl:output method="xml" encoding="UTF-8" doctype-system="sample.dtd" />
htmlを出力するとき
<xsl:output method="html" encoding="Shift_JIS" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
textを出力するとき
<xsl:output method="text" encoding="Shift_JIS" />

改行/スペースの出力方法

<xsl:text>をつかって記述する。

改行だったら以下の感じ
<xsl:text>
</xsl:text>

ただし、空白HTMLに出力する場合には Entityにすることが必要
空白 は 「&#160;」 です。

改行をbrタグに変換

<xsl:template name="br-replace">
<xsl:param name="word"/>
<xsl:choose>
<xsl:when test="contains($word,'&#xA;')">
<xsl:value-of select="substring-before($word,'&#xA;')"/>
<br/>
<xsl:call-template name="br-replace">
<xsl:with-param name="word" select="substring-after($word,'&#xA;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$word"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


.の文字を/に変換して相対パスを生成

<xsl:template name="path">
<xsl:param name="path"/>
<xsl:if test="contains($path,'.')">
<xsl:text>../</xsl:text>
<xsl:call-template name="path">
<xsl:with-param name="path"><xsl:value-of select="substring-after($path,'.')"/></xsl:with-param>
</xsl:call-template>
</xsl:if>
<xsl:if test="not(contains($path,'.')) and not($path = '')">
<xsl:text>../</xsl:text>
</xsl:if>
</xsl:template>

test句のサンプル

  • test="$count &gt; 0"
  • test="@errors[. &gt; 0]"
  • test="not(@message)"
  • test="contains($string,&quot;'quot;)"
  • test="not(contains($path,'.')) and not($path = '')"
  • test="position() != 1"
  • test="@name[.!='']
  • test="count(説明) > 0"