2014-09-26 87 views
0

我正在嘗試配置轉換。 在配置文件中我有配置轉換的XSLT模式匹配

<system.serviceModel> 
<client> 
    <endpoint address="net.pipe://localhost/someservice" ....../> 
</client> 

我需要更換 'localhost' 的使用XSLT轉換。我無法繞過使用正則表達式。

感謝,

回答

0

這個腳本應該做的伎倆:您輸入的XML文件也必須是合式

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

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

    <xsl:template match="endpoint/@address"> 
     <xsl:attribute name="address"><xsl:value-of select="replace(current(),'localhost','www.myhost.com')"/></xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

多加留意。它必須有一個包含所有其他節點的根XML節點。請參閱下面的測試源XML。我選擇了命名根節點「xml」。而節點「system.serviceModel」必須有一個結束標記。

源XML:

<xml> 
    <system.serviceModel/> 
    <client> 
     <endpoint address="net.pipe://localhost/someservice"/> 
    </client> 
</xml> 

結果XML:

<?xml version="1.0" encoding="UTF-8"?> 
<xml> 
    <system.serviceModel/> 
    <client> 
     <endpoint address="net.pipe://www.myhost.com/someservice"/> 
    </client> 
</xml> 
+2

XSLT/XPath 1.0中已經沒有'代替()'功能。 – Tomalak 2014-09-26 13:20:32

1

使用恆等變換,添加這個模板:

<xsl:template match="@address[contains(., '://localhost/')]"> 
    <xsl:attribute name="{name()}"> 
    <xsl:value-of select="substring-before(., 'localhost')" /> 
    <xsl:text>replacement value</xsl:text> 
    <xsl:value-of select="substring-after(., 'localhost')" /> 
    </xsl:attribute> 
</xsl:template> 
+0

謝謝!但是在XSLT中是否有可能替代例如。連接字符串我可能會把!(#SERVER#),!(#HOST#),然後變換從另一個XML獲得價值(這一點我可以做!)。就像在C#中,我可以做正則表達式,讀取鍵值並替換爲輸出xml。再次感謝你的幫助。 – Vivek 2014-09-28 00:50:44