Replacing URL Strings in an XML Document with XSLT
I am having trouble using the XSLT 1.0 function library (since .NET/Visual Studio doesn't support 2.0), replacing attribute strings using XSLT in my XML document.
The attributes contain URL strings, but as soon as the URLs are read in via the translate() function, everything is garbled and comes out a mess. This is most likely due to the encoding of the strings it is reading in and trying to write out. Does anyone have a suggestion for a better way to read/output the strings so that the URL's do not get garbled?
Little ba开发者_JS百科ckground on the issue. I am creating a dynamic ASP.Net menu control, and populating it from an XMLDataSource. The NavigateUrlField is set to "Url", and in the XML, the Url Field contains strings that look like this:
Url="%PLACEHOLDER1%/dir/dir2/page.aspx"
OR Url="%PLACEHOLDER2%/dir/dir2/page.aspx"I am using the XmlDataSource TransformFile property, set to my XSLT, and the OnTransforming event handler to pass parameters to the XSLT file.
What I want to do, is replace the text %PLACEHOLDER1%" and "%PLACEHOLDER2" via XSLT, so that they actually form different URLs when bound to the ASP.Net Menu control.
This is useful for me because there are different domains and different sites (local/dev/production/etc), and the domain URLs are different. In this way, I can just pass different parameters to the XSLT depending on which version I am building/testing.
Here is the XSLT code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:param name="targetString"/>
<xsl:param name="replacementString"/>
<xsl:template match="@Url">
<xsl:attribute name="Url">
<xsl:choose>
<xsl:when test="contains(., $targetString)">
<xsl:value-of select="translate(.,$targetString,$replacementString)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*|*">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And here is the code in the event handler calling the XSLT:
protected void TransformEventHandler(object sender, EventArgs e)
{
// Create an XsltArgumentList.
XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("targetString", "", "%PLACEHOLDER1%");
xslArg.AddParam("replacementString", "", "http://www.testdomain.com");
((XmlDataSource)sender).TransformArgumentList = xslArg;
}
And the XML File looks like this:
<FooterNAV>
<Menu LinkText="Link 1" Url="%PLACEHOLDER1%/dir/dir2/somepage.aspx" Description="" />
<Menu LinkText="Link 2" Description="" Url="%PLACEHOLDER1%/dir/dir2/someotherpage.aspx" />
</FooterNAV>
In these examples I am only trying to replace the PLACEHOLDER1 text, but if I could get that working I would create a second Param in the XSLT and pass it from the code-behind as well to replace the other PLACEHOLDERS in my XML.
I would really appreciate any suggestions, if you need any further information please do let me know!
OK, translate()
doesn't work like you think it does.
translate()
takes three parameters, and replaces values from the first parameter character by character, not by the entire string.
You need to use one of the more open replace templates, such as this one.
精彩评论