<xsl:template match="">の仕組みが知りたい
<xsl:template match="p">の処理内に<p>要素のテキストを抽出する<xsl:value-of select="."/>を記述しなくても、以下のXSLTでは<p>要素のテキスト部分が抽出されます。
これはなぜ(どのような仕組み)か、ご説明いただけないでしょうか。
変換元ソース:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <root> <p>回転数は1,000 min<span class="sup">-1</span>から2,000 min<span class="sup">-1</span>まで</p> </root>
変換XSLT:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <topic> <title>character test</title> <body> <xsl:apply-templates/> </body> </topic> </xsl:template> <xsl:template match="root"> <xsl:apply-templates/> </xsl:template> ** <xsl:template match="p"> <p> <xsl:apply-templates/> </p> </xsl:template>** <xsl:template match="span[@class='sup']"> <sup><xsl:value-of select="."/></sup> </xsl:template> </xsl:stylesheet>
結果:
<?xml version="1.0" encoding="UTF-8"?> <topic> <title>character test</title> <body> <p>回転数は1,000 min<sup>-1</sup>から2,000 min<sup>-1</sup>。</p> </body> </topic>
この問題の前に、以下のXSLTでxsl:value-ofを指定すると、<p>要素のテキスト部分が重複して抽出される問題に行き当たりました。
変換元ソース:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <root> <p>回転数は1,000 min<span class="sup">-1</span>以上</p> </root>
変換XSLT:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <topic> <title>character test</title> <body> <xsl:apply-templates/> </body> </topic> </xsl:template> <xsl:template match="root"> <xsl:apply-templates/> </xsl:template> <xsl:template match="p"> <xsl:variable name="text" select="."/> <p> <xsl:value-of select="substring-before($text,span)"/> <xsl:apply-templates/> <xsl:value-of select="substring-after($text,span)"/> </p> </xsl:template> <xsl:template match="span[@class='sup']"> <sup><xsl:value-of select="."/></sup> </xsl:template> </xsl:stylesheet>
結果:
<?xml version="1.0" encoding="UTF-8"?> <topic> <title>character test</title> <body> **<p>回転数は1,000 min回転数は1,000 min<sup>-1</sup>以上以上</p>** </body> </topic>
試したこと
この問題は、ChatGPTに投げてみたところ<xsl:template match="p">処理内のxsl:apply-templates/を<xsl:apply-templates select="./span"/>に変更すればOK…とあったので、そのように変更したら希望する出力が得られましたが、ここでもなぜこれで解決するのか理解できていません。
初心者の域から脱せられず、何某で読んだ「要素matchは、node()とtext()を選択している…」が何に影響を及ぼすかも今イチ理解できません。これが関係しているのでしょうか。
何か共通して欠けている理解(知識)があるように思えます。
参考にすると良いサイトがあれば、そのご紹介でも結構です。
よろしくご教授のほど、お願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/04/14 06:59