0

XML ノードの HTML コンテンツを変更して、一部の BBCode を XSL 1.0 の HTML タグに置き換えたいと考えています。

私のXMLは次のようになります:

<Article>
    <Title>Article test</Title>
    <Content>
        <![CDATA[<p>Lorem Ipsum :</p><p>"[[Lorem ipsum dolor sit amet]] is a great tool !"</p>]]>
    </Content>
</Article>

<em>"[[" を " " に、"]]" を " "に置き換えたいと思い</em>ます。

私のXSLは次のようになります:

<?xml version="1.0" encoding = "ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"
    media-type="text/html"
    indent="yes"
    omit-xml-declaration="yes"
    encoding="ISO-8859-1"
    doctype-public="-//W3C//DTD HTML 4.0 Strict//FR"
    />
    <xsl:template match="/" >
<html>
    <head>
        <!--...-->
    </head>
    <body>
<xsl:for-each select="//Article[1]">
                        <h2><xsl:value-of select="Title"/></h2>
                        <div class="content"><xsl:apply-templates select="Content"/></div>
</xsl:for-each>
    </body>
</html>
    </xsl:template><!-- match=/ -->
    <xsl:template match="Content">
        <xsl:variable name="normaContenu"><xsl:value-of select="normalize-space(.)" /></xsl:variable>
        <xsl:variable name="replacePatternStart"><![CDATA[<em>]]></xsl:variable>
        <xsl:variable name="replacePatternEnd"><![CDATA[</em>]]></xsl:variable>
        <xsl:variable name="contenuWithStartTags">
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="$normaContenu" />
                <xsl:with-param name="replace" select="'[['" />
                <xsl:with-param name="by" select="string($replacePatternStart)" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="replace">
            <xsl:with-param name="text" select="string($contenuWithStartTags)" />
            <xsl:with-param name="replace" select="']]'" />
            <xsl:with-param name="by" select="string($replacePatternEnd)" />
        </xsl:call-template>
    </xsl:template><!-- match=Content -->
    <xsl:template name="replace">
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="replace">
                    <xsl:with-param name="text" select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template><!-- name=replace -->
</xsl:stylesheet>

問題 : " <em>" は実際には " &lt;em&gt;" です。CDATA で定義されている文字を XSL に保持させるにはどうすればよいですか?

ありがとうございました !

4

1 に答える 1

0

あなたのコードに従うのは簡単ではありません。置き換えは実際には <xsl:value-of select="..."/> のどこかで行われます - ここでは代わりに <xsl:value-of select="..." disable-output-escaping="yes"/> を使用します。

@Pierre Morin: <em> タグは挿入時にエスケープされる可能性があるため、disable-output-escaping は効果を示しません。ブラウザーが異なれば、変換の処理が異なる可能性があり、さまざまな間違いが発生する可能性があります。出力のみを含む小さなソリューションを試して、それが機能するときにこれに合わせてください。SaxonCE (無料のクライアント側 XSLT 1.0 および 2.0 エンジン、javascript) や libexslt (無料のサーバー側 XSLT 1.0 エンジン、c で書かれ、php などで広く使用されている) のような変換エンジンを使用することをお勧めします
。別のコードのタグ。XSLT はそのために構築されていません。要素を壊さずに操作できますが、これには XSLT の概念を理解するのに時間がかかります。一度手に入れると強力です。
これがあなたのニーズに合っているかどうかはわかりませんが、 <em> に $text を入れるのは簡単です

<xsl:template match="...">
  <em>  
    <xsl:value-of select="$text"/>
  </em>  
</xsl:template>
于 2013-10-10T09:03:47.740 に答える