I have the following line of code in a HTML file (or something similar):
...
<a href="#SCRIPT_NAME#?a=b&id=a/b/c/d">Link Content</a>
...
I need to be able to extract the a/b/c/d
part of the href
and convert the link to something like:
<a href="/lookup?id=a/b/c/d">Link Content</a>
Ideally I'd like to be able to do this with regex, but most of the regex stuff I've seen for XSLT on StackOverflow seems to require XPath 2.
Ah yes... I'm using SimpleXML/DomDocument on PHP5.3 to apply the stylesheet which I believe doesn't support v2 xslt.
I think I could do string replacement to lose the first part, but I'd like to have a pattern match to extract it.
Any thoughts?
As already pointed out in the answer given by michael.hor257k, you have to adjust the &
character to have valid XML. Given an input containing for example
<a href="#SCRIPT_NAME#?a=b&id=a/b/c/d">Link Content</a>
the following template
<xsl:template match="a/@href[starts-with(.,'#SCRIPT_NAME#')]">
<xsl:attribute name="href">
<xsl:value-of select="concat('/lookup?id=', substring-after(.,'id='))"/>
</xsl:attribute>
</xsl:template>
changes the link to
<a href="/lookup?id=a/b/c/d">Link Content</a>
matching every href
starting with #SCRIPT_NAME#
.
Though it's not clear from the question which is the part that has to be matched / how to identify the links that have to be adjusted, possibly you can adjust this example to fit your requirements or provide further input to your question.