- - PR -
XSLTで2つのテンプレート(改行・上付き文字)を反映させたい
1
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2004-06-23 14:35
XSLT初心者です。よろしくお願いします。
以下のXMLで「改行」と「文字修飾(立法メートルなどの上付き文字)」を認識してhtmlへ表示させたいのですが、テンプレートの競合にひっかかって上手くいきません。 XML例: <Root> <contents>体積・・・100m<ue>3</ue> 面積・・・10m<ue>2</ue> 全周・・・40m</contents> </Root> 作成してみたXSLT: <xsl:template match="/"> <html> <head> <title>test</title> </head> <body> <xsl:call-template name="replace"> <xsl:with-param name="para"> <xsl:apply-templates select="Root/contents" /> </xsl:with-param> </xsl:call-template> </body> </html> </xsl:template> <!-- 自動改行テンプレート --> <xsl:template name="replace"> <xsl:param name="para"/> <xsl:choose> <xsl:when test="contains($para,' ')"><xsl:value-of select="substring-before($para,' ')"/><br /> <xsl:call-template name="replace"> <xsl:with-param name="para" select="substring-after($para,' ')"/> </xsl:call-template> </xsl:when> <xsl:otherwise><xsl:value-of select="$para"/></xsl:otherwise> </xsl:choose> </xsl:template> <!-- 上付き文字テンプレート --> <xsl:template match="ue"> <sup> <xsl:value-of select="."/> </sup> </xsl:template> </xsl:stylesheet> </Root> このXSLTでは、「改行」のみしか反映されません。 XMLのカスタマイズですが、一行ずつ<contents>タグでくくることは不可能です。 タグ名を<ue>→<sup>に変更することは可能ですが、変更してXSLTでcopy-ofを使ってみたりしましたが、上手くいきませんでした。 何か、よい方法はないでしょうか? ご教授いただければ、助かります。。 |
|
投稿日時: 2004-06-23 16:18
競合など起きていません。
<xsl:apply-templates select="Root/contents"/> で 体積・・・100m<ue>3</ue> が 体積・・・100m<sup>3</sup> になり、その後 <xsl:call-template name="replace"> により、<sup> が消えているだけです。 ====================================== 追加 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <html> <head> <title>test</title> </head> <body> <xsl:apply-templates select="Root/contents"/> </body> </html> </xsl:template> <!-- 置換に引き込み ラフな選択ですが --> <xsl:template match="text()"> <xsl:call-template name="replace"> <xsl:with-param name="para" select="."/> </xsl:call-template> </xsl:template> <!-- 自動改行テンプレート --> <!-- 処理速度を期待するな --> <xsl:template name="replace"> <xsl:param name="para"/> ---- <xsl:choose> <xsl:when test="contains($para,' ')"> <xsl:value-of select="substring-before($para,' ')"/> <br/> <xsl:call-template name="replace"> <xsl:with-param name="para" select="substring-after($para,' ')"/> </xsl:call-template> </xsl:when> <xsl:otherwise>+ <xsl:value-of select="$para"/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- 上付き文字テンプレート --> <xsl:template match="ue"> <sup> <xsl:value-of select="."/> </sup> </xsl:template> </xsl:stylesheet> ======= 注: XMLSpy 内蔵の XSLTエンジンは 改行を としている。 +や ---- はデバッグの確認用 [ メッセージ編集済み 編集者: MMX 編集日時 2004-06-23 16:34 ] |
|
投稿日時: 2004-06-23 17:52
MMXさま、ご親切にありがとうございます。
競合ではなかったんですね・・・知識が乏しくて、お恥ずかしい限りです。 色々試してはいたのですが、全くアサッテの方向に進んでいました。 ご教授頂き、非常に助かりました。参考にして頑張ります。 |
1