2014-09-26 88 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