Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT - extracting data from an element 1

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

I have the following element - http:host/path/110209ATDdigitaldailyFINAL_115x65.jpg|115|65|

I need to extract the three parts of this image data - the url, width and height –

- http:host/path/110209ATDdigitaldailyFINAL_115x65.jpg
- 115
- 65

Any suggestions?

Much appreciated,
Dan
 
Good Day,

I ended up doing the following -


Code:
        <xsl:variable name="runner"        select="."/>

        <xsl:variable name="url"           select="substring-before($runner,'|')"/>

        <xsl:variable name="url-after"     select="substring-after($runner,'|')"/>               <!-- would have - 512|288| -->
        <xsl:variable name="width"         select="substring-before($url-after,'|')"/>

        <xsl:variable name="width-after"   select="substring-after($url-after,'|')"/>            <!-- would have - 288| -->
        <xsl:variable name="height"        select="substring-before($width-after,'|')"/>

Regards,
Dan
 
[0] There is some fine-detail that can cause surprised outcome. Suppose the data does not contain a separator, or that the data is missing, say the last one and without the trailing separator, correct result will not be returned. I would suggest taking care of that as well.

[1] This is another rendering of the same functionality with that detail taken care of as well.
[1.1] first two named templates
[tt]
<xsl:template name="s-before">
<xsl:param name="s" />
<xsl:param name="sep" />
<xsl:choose>
<xsl:when test="contains($s,$sep)">
<xsl:value-of select="substring-before($s,$sep)" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="$s" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
<xsl:template name="s-after">
<xsl:param name="s" />
<xsl:param name="sep" />
<xsl:choose>
<xsl:when test="contains($s,$sep)">
<xsl:value-of select="substring-after($s,$sep)" />
</xsl:when>
<xsl:eek:therwise>
<!-- or some alternative default value, you've the freedom here -->
<xsl:value-of select="''" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
[/tt]
[1.2] The main logic flows like this.
[tt]
<xsl:variable name="runner" select="." />
<xsl:variable name="url">
<xsl:call-template name="s-before">
<xsl:with-param name="s" select="$runner" />
<xsl:with-param name="sep" select="'|'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="url-after">
<xsl:call-template name="s-after">
<xsl:with-param name="s" select="$data" />
<xsl:with-param name="sep" select="'|'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="width">
<xsl:call-template name="s-before">
<xsl:with-param name="s" select="$url-after" />
<xsl:with-param name="sep" select="'|'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="width-after">
<xsl:call-template name="s-after">
<xsl:with-param name="s" select="$url-after" />
<xsl:with-param name="sep" select="'|'" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="height">
<xsl:call-template name="s-before">
<xsl:with-param name="s" select="$width-after" />
<xsl:with-param name="sep" select="'|'" />
</xsl:call-template>
</xsl:variable>
[/tt]
[2] The difference can be seen using sample (or some variation of it containing only width but without trailing "|" etc) like this.
[tt] <xsl:variable name="runner" select="'http:host/path/110209ATDdigitaldailyFINAL_115x65.jpg'" />
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top