2012-01-23 28 views
1

を使用してXML内の文字列の一部を置き換えます。私は次のXML持っXSL

<links url="../servlet/SearchServlet?query=contact&filter=&sort=relevance&sortdir=desc&col=5&startdate=0&enddate=0&xsl=xml&page="> 

<link page="date" url="../servlet/SearchServlet?query=contact&filter=&sort=date&col=5&startdate=0&enddate=0&page=1&xsl=xml"/> 

を私はリンクを変換するために、このXSLを使用しています:

<xsl:for-each select="//link"> 

      <xsl:choose> 
       <xsl:when test="@page='next'"> 
        <xsl:text></xsl:text> 
        <a href="{@url}">&gt;&gt;</a> 
       </xsl:when> 
       <xsl:when test="@page='prev'"> 
        <a href="{@url}">&lt;&lt;</a> 
        <xsl:text></xsl:text> 
       </xsl:when> 

       <xsl:when test="@page='date'"/> 
       <xsl:when test="@page='relevance'"/> 
       <xsl:when test="@page='alpha'"/> 

      </xsl:choose> 
     </xsl:for-each> 

トリッキーな部分は、今私がしたいですリンク内のurlの一部を../servlet/SearchServlet../search

から置き換えてください。私はusing this templateを試しましたが、それは全体のurlを置き換えます。あなたは、必要に応じて別の検索/置換値を供給する

<xsl:apply-templates select="@url" /> 
    <xsl:with-param name="search" select="'xyz'" /> 
    <xsl:with-param name="replace" select="'foo'" /> 
</xsl:apply-templates> 

を使用することができます

+0

リンク先テンプレートの使用方法を示してください。 –

答えて

5
<xsl:template match="link/@url"> 
    <xsl:param name="search" select="'../servlet/SearchServlet'" /> 
    <xsl:param name="replace" select="'../search'" /> 
    <xsl:attribute name="href"> 
    <xsl:choose> 
     <xsl:when test="contains(., $search)"> 
     <xsl:value-of select="concat($replace, substring-after(., $search))" /> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="." /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:attribute> 
</xsl:template> 

コール

<a> 
    <xsl:apply-templates select="@url" /> 
    <xsl:text>&lt;&lt;</xsl:text> 
</a> 

ように注意してください。

+0

私が探していたもの。ありがとう! – robasta

+0

Tomalak、あなたはsimlerともっと短い解決策を見たいと思うかもしれません:) –

+0

@Dimitre私は一般的な変種を目指していました。しかし、はい、AVTsは、特定のケースのためのより良いです。 – Tomalak

4

これは、明示的な条件命令やxsl:attributeを使用していない、単純で短く、完全なソリューションです:

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

<xsl:param name="pTarget" select="'../servlet/SearchServlet'"/> 
<xsl:param name="pRep" select="'../search'"/> 

<xsl:template match="link"> 
    <xsl:variable name="vCUrl" select="concat(@url, $pTarget)"/> 

    <a url="{substring-before($vCUrl,$pTarget)}{$pRep}{substring-after(@url,$pTarget)}"> 
    <xsl:apply-templates select="@page"/> 
    </a> 
</xsl:template> 

<xsl:template match="link/@page[. = 'next']">>></xsl:template> 
<xsl:template match="link/@page[. = 'prev']">&lt;&lt;</xsl:template> 

<xsl:template match="link[not(@page='next' or @page='prev')]"/> 
</xsl:stylesheet> 

次のXML文書に適用された場合:

<links> 
<link page="prev" url="../servlet/SearchServlet?query=contact&amp;filter=&amp;sort=relevance&amp;sortdir=desc&amp;col=5&amp;startdate=0&amp;enddate=0&amp;xsl=xml&amp;page="/> 
<link page="date" url="../servlet/SearchServlet?query=contact&amp;filter=&amp;sort=date&amp;col=5&amp;startdate=0&amp;enddate=0&amp;page=1&amp;xsl=xml"/> 
<link page="next" url="../servlet/SearchServlet?query=contact&amp;filter=&amp;sort=date&amp;col=5&amp;startdate=0&amp;enddate=0&amp;page=1&amp;xsl=xml"/> 
</links> 

望ましい結果が得られる

<a url="../search?query=contact&amp;filter=&amp;sort=relevance&amp;sortdir=desc&amp;col=5&amp;startdate=0&amp;enddate=0&amp;xsl=xml&amp;page=">&lt;&lt;</a> 
<a url="../search?query=contact&amp;filter=&amp;sort=date&amp;col=5&amp;startdate=0&amp;enddate=0&amp;page=1&amp;xsl=xml">&gt;&gt;</a> 

説明

  1. AVT(Attribute Value Templates

  2. パターンマッチングとテンプレートの適切な使用を適切に使用します。

  3. 条件付き処理が不要になるように、センチネルを文字列に追加します。

関連する問題