2013-05-08 53 views
0

我正在嘗試從XML中更改一些文本。但它沒有奏效。使用XSLT更改文本

我要顯示所有從XML [intro]文本的某些Path(text)顯示[intro]之前應該改變。

例如

<a href="3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf"> --> wrong path 

我想改變

<a href="Content\3\D\D\3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf"> 

任何幫助將不勝感激。

示例XML

<?xml version="1.0"?> 
<root> 
    <intro xml:lang="en"> 
    <div class="blueBar"> 
     <h2>Highlights</h2> 
     <ul> 
     <li><a href="http://xxx/xxx/default.asp?lang=En">aaaa</a></li> 
     <li><a href="http://xxx/default.asp?lang=En">bbbb</a></li> 
     <li><a href="http://xxx/Content/1/C/D/1CD1DFC3-5149-4D61-A7C3-2D9CF7E65F8C/rrr.pdf">ccc</a></li> 
     <li><a href="3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf">pdf</a></li> 
     </ul> 
    </div> 
    </intro> 
    <intro> .....</intro> 
</root> 

樣品XSLT

<xsl:param name="language"/> 

<xsl:template match="root"> 
    <xsl:for-each select="intro[lang($language)]//@href"> 
    <xsl:choose> 
     <xsl:when test="contains(.,'pdf') and not(contains(.,'Content'))"> 
     <xsl:variable name="pdfGuid"> 
      <xsl:value-of select="substring(.,0,36)"/> 
     </xsl:variable> 
     <xsl:variable name="pdfPath"> 
      <xsl:value-of select="concat('&#47;','Content')"/> 
      <xsl:value-of select="concat('&#47;', substring($pdfGuid, 1,1))"/> 
      <xsl:value-of select="concat('&#47;', substring($pdfGuid, 2,1))"/> 
      <xsl:value-of select="concat('&#47;', substring($pdfGuid, 3,1))"/> 
      <xsl:value-of select="concat('&#47;', $pdfGuid)"/> 
     </xsl:variable>  
     <xsl:value-of select="strJS:replace(string(.),string($pdfGuid),string($pdfPath))" disable-output-escaping="yes"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="." disable-output-escaping="yes"/> 
     </xsl:otherwise> 
    </xsl:choose>  
    </xsl:for-each> 

    <div class="greenBox"> 
    <xsl:value-of select="intro[lang($language)]" disable-output-escaping="yes"/> 
    </div>  

</xsl:template> 
+0

的XSLT是沒有意義的。什麼是'do?你爲什麼使用'disable-output-escaping'?你可以做一個小的,充分的,充分的例子你有什麼作爲XML和你想要在輸出。 – kan 2013-05-08 21:16:45

回答

0

當你問這裏的一個問題,請解釋清楚你想要的輸出,什麼問題是,您遇到。

您對問題的描述與您的嘗試之間存在差異,其中您正在使用正斜槓,並且在XSLT中路徑的開始處有一個斜槓,並且在描述中使用了反斜槓。我會假設你想要正斜槓,但這些可以很容易地改變。我懷疑你想要的是這樣的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:param name="language" select="'en'" /> 

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

    <xsl:template match="/*"> 
    <div class="greenBox"> 
     <xsl:apply-templates select="intro[lang($language)]/node()" /> 
    </div> 
    </xsl:template> 

    <xsl:template match="@href[contains(., 'pdf') and not(contains(., 'Content'))]"> 
    <xsl:attribute name="href"> 
     <xsl:value-of select="concat('/Content/', 
            substring(., 1, 1), '/', 
            substring(., 2, 1), '/', 
            substring(., 3, 1), '/', 
            .)"/> 
    </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行,其結果是:

<div class="greenBox"> 
    <div class="blueBar"> 
     <h2>Highlights</h2> 
     <ul> 
     <li> 
      <a href="http://xxx/xxx/default.asp?lang=En">aaaa</a> 
     </li> 
     <li> 
      <a href="http://xxx/default.asp?lang=En">bbbb</a> 
     </li> 
     <li> 
      <a href="http://xxx/Content/1/C/D/1CD1DFC3-5149-4D61-A7C3-2D9CF7E65F8C/rrr.pdf">ccc</a> 
     </li> 
     <li> 
      <a href="/Content/3/D/D/3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf">pdf</a> 
     </li> 
     </ul> 
    </div> 
    </div>