2

私はこのXMLコードを持っています

<parag>blah blah blah</parag>
<parag>blah blah, refer to <linkCode code1="a" code2="b" code3="c"/> for further details.</parag>

リンクを親テキストの途中に留める方法がわかりません。次のコード

<xsl:for-each select="parag">
    <p><xsl:value-of select="text()"/>
        <xsl:for-each select="linkCode">
            <a href="file-{@code1}-{@code2}-{@code3}.html">this link</a>
        </xsl:for-each>
    </p>
</xsl:for-each>

を生成します

<p>blah blah blah</p>
<p>blah blah, refer to for further details.<a href="file-a-b-c.html">this link</a></p>

私が欲しいのは

<p>blah blah blah</p>
<p>blah blah, refer to <a href="file-a-b-c.html">this link</a> for further details.</p>

何か案は?いいえ、XMLのコンテンツを制御することはできません。

4

1 に答える 1

1

ID ルールの単純なオーバーライドのみを使用します

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

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="linkCode">
   <a href="file-{@code1}-{@code2}-{@code3}.html">this link</a>
 </xsl:template>

 <xsl:template match="parag">
   <p><xsl:apply-templates select="node()|@*"/></p>
 </xsl:template>
</xsl:stylesheet>

この変換が次の XML ドキュメントに適用されると(提供されたフラグメントが 1 つの最上位要素にラップされ、整形式の XML ドキュメントが取得されます):

<t>
    <parag>blah blah blah</parag>
    <parag>blah blah, refer to <linkCode code1="a" code2="b" code3="c"/> for further details.</parag>
</t>

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

<t>
    <p>blah blah blah</p>
    <p>blah blah, refer to <a href="file-a-b-c.html">this link</a> for further details.</p>
</t>

そして、一番上の要素を出力しないようにしたい場合

このテンプレートを追加するだけです:

 <xsl:template match="/*"><xsl:apply-templates/></xsl:template>

したがって、完全なコードは次のようになります。

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

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="linkCode">
   <a href="file-{@code1}-{@code2}-{@code3}.html">this link</a>
 </xsl:template>

 <xsl:template match="parag">
   <p><xsl:apply-templates select="node()|@*"/></p>
 </xsl:template>
 <xsl:template match="/*"><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>
于 2012-12-07T14:20:32.923 に答える