C# 使用XSL替换XML中字符串的一部分
我有以下xml:C# 使用XSL替换XML中字符串的一部分,c#,xml,xslt,replace,C#,Xml,Xslt,Replace,我有以下xml: <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&fi
<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}">>></a>
</xsl:when>
<xsl:when test="@page='prev'">
<a href="{@url}"><<</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>
现在棘手的部分是我想替换。/servlet/SearchServlet
到。/search
链接中的url
的一部分
我如何做到这一点?我试过了,但它替换了整个url
<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><<</xsl:text>
</a>
注意,您可以选择使用
<xsl:apply-templates select="@url" />
<xsl:with-param name="search" select="'xyz'" />
<xsl:with-param name="replace" select="'foo'" />
</xsl:apply-templates>
提供不同的搜索/替换值。
称为
<a>
<xsl:apply-templates select="@url" />
<xsl:text><<</xsl:text>
</a>
注意,您可以选择使用
<xsl:apply-templates select="@url" />
<xsl:with-param name="search" select="'xyz'" />
<xsl:with-param name="replace" select="'foo'" />
</xsl:apply-templates>
提供不同的搜索/替换值。这是一个更简单、更短、完整的解决方案,不使用任何显式条件指令或
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']"><<</xsl:template>
<xsl:template match="link[not(@page='next' or @page='prev')]"/>
</xsl:stylesheet>
)
适当使用模式匹配和模板
将sentinel添加到字符串中,这样就不需要条件处理了
这是一个更简单、更短、完整的解决方案,不使用任何显式条件指令或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']"><<</xsl:template>
<xsl:template match="link[not(@page='next' or @page='prev')]"/>
</xsl:stylesheet>
)
适当使用模式匹配和模板
将sentinel添加到字符串中,这样就不需要条件处理了
请展示您如何尝试使用链接模板。请展示您如何尝试使用链接模板。这正是我要找的。谢谢Tomalak,你可能有兴趣看到一个更简单、更简短的解决方案:)@Dimitre我的目标是一个通用的变体。但是,是的,AVT更适合具体情况。Tomalak,在什么意义上你的解决方案比我的更通用?@Dimitre好吧,你可以说它是推式的,因此更模块化和可重用,而且它允许通过模板参数进行不同的替换。这正是我想要的。谢谢Tomalak,你可能有兴趣看到一个更简单、更简短的解决方案:)@Dimitre我的目标是一个通用的变体。但是,是的,AVT更适合具体情况。Tomalak,在什么意义上你的解决方案比我的更通用?@Dimitre嗯,你可以说它是推式的,因此更模块化和可重用,而且它允许通过模板参数进行不同的替换。