I’m trying to write a function which gets the domain name from a URL text in XML file i.e http://www.example.com.
<xsl:function name="fdd:get-domain">
<xsl:param name="url"/>
<xsl:analyze-string select="$url" regex="^(.*)://([a-zA-Z0-9\-\.]?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?)(/.*)$">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="false()"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
This function always returns false. I’m not sure what am I missing in this.
Inside an attribute value every
{and}must be doubled (in order to distinguish them from the single chars that denote an AVT. Just by doubling the curly braces:with this correction, when called like this:
the result is:
I guess that you really want to get the domain, as this modified code (both the regex expression and the regex-group index) does:
When this transformation is applied on any XML document (not used), the wanted, correct result is produced:
Update: As reminded by Michael Kay, the need to duplicate any curly braces can be avoided if the RegEx is specified as the context of a variable and this variable is referenced as an AVT in the
regexattribute ofxsl:analyze-string:This has another benefit — we can split RegEx subexpressions on different lines and even intermix them with comments.
Here is the refactored transformation: