Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Asif Malik 203 posts 339 karma points
    Sep 30, 2010 @ 08:55
    Asif Malik
    0

    regex in xslt

    I have been trying to use regex to replace the height and width paramters of a you tube embedded video. I have successuflly managed to get the regex to replace the first instance of the height and width, however it wont change the second instance. I am using the gi flags to replace globally and ignore case .

    As you will see in the code below i have also tried to replace the values for a second time in case the g flag wasnt working

     

       <xsl:template name="getVideo">
    <xsl:param name="propertyAlias" />
    <xsl:param name="startNode" select="$currentPage" />
    <xsl:param name="height" />
    <xsl:param name="width" />

    <xsl:if test="string-length($startNode/data[@alias = $propertyAlias]) != 0">
    <xsl:variable name="mediaItemXML" select="umbraco.library:GetMedia($startNode/data[@alias = $propertyAlias] , 0)" />

    <xsl:variable name="replacementHeight" select="concat('height=&quot;' , $height , '&quot;')"/>
    <xsl:variable name="replacementWidth" select="concat('width=&quot;' , $width , '&quot;')"/>

    <xsl:variable name="replacedHeight1" select="Exslt.ExsltRegularExpressions:replace($mediaItemXML/data[@alias = 'embedCode'] , 'height=&quot;\d+&quot;' , 'gi' , $replacementHeight)" />
    <xsl:variable name="replacedWidth1" select="Exslt.ExsltRegularExpressions:replace($replacedHeight1 , 'width=&quot;\d+&quot;', 'gi', $replacementWidth)" />

    <xsl:variable name="replacedHeight2" select="Exslt.ExsltRegularExpressions:replace($replacedWidth1 , 'height=&quot;\d+&quot;' , 'gi' , $replacementHeight)" />
    <xsl:variable name="replacedWidth2" select="Exslt.ExsltRegularExpressions:replace($replacedHeight2 , 'width=&quot;\d+&quot;', 'gi', $replacementWidth)" />

    <xsl:if test="string-length($mediaItemXML) != 0">
    <xsl:value-of select="$replacedWidth2" disable-output-escaping="yes" />
                            </xsl:if>
    </xsl:if>
    </xsl:template>

     

    The html that gets outputted from the above is

    <object width="266" height="174"><param name="movie" value="http://www.youtube.com/v/eut6r2LTx7I?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/eut6r2LTx7I?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>

     

    If i remove the disable-output-escaping then i can see that the html being outputted is

    &lt;object width="266" height="174"&gt;&lt;param name="movie" value="http://www.youtube.com/v/eut6r2LTx7I?fs=1&amp;amp;hl=en_US"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/eut6r2LTx7I?fs=1&amp;amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"&gt;&lt;/embed&gt;&lt;/object&gt;

     

    Any help is much appreciated

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 30, 2010 @ 09:39
    Sebastiaan Janssen
    0

    I don't know if there are any regex experts on here, so I doubt this forum will be of much help. Have a look at regexr though, awesome site. And if nobody can help you here, I'm sure the folks at stack overflow can be of assistance.

  • Asif Malik 203 posts 339 karma points
    Sep 30, 2010 @ 09:41
    Asif Malik
    0

    Thanks for you reply, once i have found an answer will post it here as well

  • Rob Watkins 369 posts 701 karma points
    Feb 23, 2011 @ 11:37
    Rob Watkins
    0

    Just found this while doing some matching, you probably solved it months ago, but for anyone else, it's because the 'g' flag for the Exslt replace function works the wrong way round:

    <xsl:value-of select="regex:replace(string('a b c a b c'), 'a', 'i', '1')"/>

    gives: 1 b c 1 b c

    <xsl:value-of select="regex:replace(string('a b c a b c'), 'a', 'ig', '1')"/>

    gives: 1 b c a b c

    The 'i' flag works as expected.

Please Sign in or register to post replies

Write your reply to:

Draft