2016-06-17 48 views
0

下のxmlの2つのURLのハイパーリンクを作成しようとしています。 私の入力XMLメッセージはこれです:後述するように、私は、XSLを通じてXMLメッセージを入れxsl複数のURLにハイパーリンクを追加しようとしています

<comment xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <biid> 
     311 
    </biid> 
    <addComment> 
     &lt;p&gt;www.test.com works another www.test.com doesn&amp;#39;t work&lt;/p&gt; 
    </addComment> 
</comment> 

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" /> 
    <xsl:template match="*/addComment"> 
     <xsl:choose> 
      <xsl:when test="contains(.,'http://')=true()"> 
       <addComment> 
        <xsl:value-of select="substring-before(.,'http://')" /> 
        <xsl:value-of select="'&amp;lt;a target=&amp;quot;_blank&amp;quot; href=&amp;quot;'" disable-output-escaping="yes" /> 
        <xsl:choose> 
         <xsl:when test="not(contains(substring-after(.,'http://'),' '))"> 
          <xsl:value-of 
           select="concat('http://',substring-before(substring-after(.,'http://'),'&lt;'))" /> 
         </xsl:when> 
         <xsl:otherwise> 
         <xsl:value-of 
           select="concat('http://',substring-before(substring-after(.,'http://'),' '))" /> 
         </xsl:otherwise> 
         </xsl:choose> 
        <xsl:value-of select="'&amp;quot;&amp;gt;'" disable-output-escaping="yes"/> 
        <xsl:choose> 
         <xsl:when test="not(contains(substring-after(.,'http://'),' '))"> 
          <xsl:value-of 
           select="concat('http://',substring-before(substring-after(.,'http://'),'&lt;'))" /> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of 
           select="concat('http://',substring-before(substring-after(.,'http://'),' '))" /> 
         </xsl:otherwise> 
        </xsl:choose> 
        <xsl:value-of select="'&lt;/a&gt;'" /> 
        <xsl:value-of 
         select="concat(' ',substring-after(substring-after(.,'http://'),' '))" /> 
       </addComment> 

      </xsl:when> 
      <xsl:when test="contains(.,'www.')=true()"> 
       <addComment> 
        <xsl:value-of select="substring-before(.,'www.')" /> 
        <xsl:value-of select="'&amp;lt;a target=&amp;quot;_blank&amp;quot; href=&amp;quot;'" disable-output-escaping="yes" /> 
        <xsl:choose> 
         <xsl:when test="not(contains(substring-after(.,'www.'),' '))"> 
          <xsl:value-of 
           select="concat('http://www.',substring-before(substring-after(.,'www.'),'&lt;'))" /> 
         </xsl:when> 
         <xsl:otherwise> 
         <xsl:value-of 
           select="concat('http://www.',substring-before(substring-after(.,'www.'),' '))" /> 
         </xsl:otherwise> 
         </xsl:choose> 
        <xsl:value-of select="'&amp;quot;&amp;gt;'" disable-output-escaping="yes"/> 
        <xsl:choose> 
         <xsl:when test="not(contains(substring-after(.,'www.'),' '))"> 
          <xsl:value-of 
           select="concat('http://www.',substring-before(substring-after(.,'www.'),'&lt;'))" /> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of 
           select="concat('http://www.',substring-before(substring-after(.,'www.'),' '))" /> 
         </xsl:otherwise> 
        </xsl:choose> 
        <xsl:value-of select="'&lt;/a&gt;'" /> 
        <xsl:value-of 
         select="concat(' ',substring-after(substring-after(.,'www.'),' '))" /> 
       </addComment> 
      </xsl:when> 
      <xsl:otherwise> 
       <addComment> 
        <xsl:apply-templates /> 
       </addComment> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

しかし、唯一の1つのURLがハイパーリンクしている結果のxml:

<?xml version="1.0" encoding="UTF-8"?><comment xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <biid> 
     311 
    </biid> 
    <addComment> 
     &lt;p&gt;&lt;a target=&quot;_blank&quot; href=&quot;http://www.test.com&quot;&gt;http://www.test.com&lt;/a&gt; works another www.test.com doesn&amp;#39;t work&lt;/p&gt; 
    </addComment> 
</comment> 

最初のwww.test.comはハイパーリンクされており、2番目のwww.test.comはハイパーリンクされていません。どのように私はそれらの両方がハイパーリンクされて達成するために管理することができますか?

+0

XSLTプロセッサを使用している:

ここでは、あなたの出発点として使用することができます簡単な例ですか? –

+0

@michael私はibisアダプタフレームワークを使用していますが、このフレームワークはxalan xsltプロセッサを使用しています。 – fasln01

+0

なぜあなたのスタイルシートは 'version =" 2.0 "'と言っていますか? XalanはXSLT 1.0のみのプロセッサです。 - ちなみに、可能であれば、正規表現を使用できるXSLT 2.0プロセッサに移行することをお勧めします。そうしないと、 'http:'、 '' www.'と 'http:// www.'である。 –

答えて

0

提供された文字列に複数のURLを処理する場合は、再帰的テンプレートを使用する必要があります。

XSLT 1.0

<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:strip-space elements="*"/> 

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

<xsl:template match="addComment"> 
    <xsl:copy> 
     <xsl:call-template name="URLs-to-links"> 
      <xsl:with-param name="text" select="."/> 
     </xsl:call-template>  
    </xsl:copy> 
</xsl:template> 

<xsl:template name="URLs-to-links"> 
    <xsl:param name="text"/> 
    <xsl:param name="open" select="'www.'"/> 
    <xsl:param name="close" select="' '"/> 
    <xsl:choose> 
     <xsl:when test="contains($text, $open) and contains(substring-after($text, $open), $close)"> 
      <xsl:value-of select="substring-before($text, $open)"/> 
      <xsl:text>&lt;a target="_blank" href="</xsl:text> 
      <xsl:value-of select="$open"/> 
      <xsl:value-of select="substring-before(substring-after($text, $open), $close)"/> 
      <xsl:text>"&gt;</xsl:text> 
      <xsl:value-of select="$open"/> 
      <xsl:value-of select="substring-before(substring-after($text, $open), $close)"/> 
      <xsl:text>&lt;/a&gt; </xsl:text> 
      <!-- recursive call --> 
      <xsl:call-template name="URLs-to-links"> 
       <xsl:with-param name="text" select="substring-after(substring-after($text, $open), $close)"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 
関連する問題