• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

Extracting images based on last digit after substring with last _

Hi Experts,

I am working on ASP.net2.0 using C#

I need to extract images from my below xml
<?xml version="1.0" encoding="utf-8" ?>
<news type="detail">
	<article id="757236">
		<images>
			<image id="757233" type="M">
				<src><![CDATA[image01_tcm133-757233.jpg]]></src>
			</image>
			
			<image id="757231" type="M">
				<src><![CDATA[abc_02_tcm133-757231.jpg]]></src>
			</image>
			<image id="757232" type="M">
				<src><![CDATA[abc_xyz_tcm133-757232.jpg]]></src>
			</image>
			<image id="757233" type="M">
				<src><![CDATA[def3_tcm133-757233.jpg]]></src>
			</image>
			<image id="757234" type="M">
				<src><![CDATA[hig_htr3_thumb_tcm133-757234.jpg]]></src>
			</image>
		</images>
	</article>
</news>

Open in new window


to bring results as follows:

<div class="first2images">
<first>image01_tcm133-757233.jpg</first>
<second>abc_02_tcm133-757231.jpg</second>    
</div>

<div class="otherimages">
    abc_xyz_tcm133-757232.jpg
    def3_tcm133-757233.jpg
    xyz_thumb03_tcm133-757234.jpg
    hig_htr3_thumb_tcm133-757234.jpg
</div>

Open in new window


I mean, from images name, need to find last underscore ("_")

if before last underscore we will see the image name will be as follows:
      image01
      abc_02

      abc_xyz
      def3
      xyz_thumb03
      hig_htr3
      
      
the image name where in last 1 comes
should come in first node
<first>

image name where 2 comes, should come in node
<second>

and rest all other images to come in node
<div class="otherimages">


Please help me in this complicated xslt

Thanks
0
tia_kamakshi
Asked:
tia_kamakshi
  • 4
  • 4
1 Solution
 
Geert BormansCommented:
Here is what I would do
Please check if the corner cases are what you want

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:output indent="yes"/>
    <xsl:template match="images">
        <div class="first2images">
            <first>
                <xsl:apply-templates select="image[1]" mode="first"/>
            </first>
            <second>
                <xsl:apply-templates select="image[1]" mode="second"/>
            </second>    
        </div>
        
        <div class="otherimages">
            <xsl:apply-templates select="image[1]" mode="other"/>
        </div>
    </xsl:template>
    
    <xsl:template match="image" mode="first">
        <xsl:variable name="name">
            <xsl:call-template name="find-name">
                <xsl:with-param name="src" select="src"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="translate(substring($name, string-length($name), 1), '1', '') = ''">
                <xsl:value-of select="src"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="following-sibling::image[1]" mode="first"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="image" mode="second">
        <xsl:variable name="name">
            <xsl:call-template name="find-name">
                <xsl:with-param name="src" select="src"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="translate(substring($name, string-length($name), 1), '2', '') = ''">
                <xsl:value-of select="src"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="following-sibling::image[1]" mode="second"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="image" mode="other">
        <xsl:variable name="name">
            <xsl:call-template name="find-name">
                <xsl:with-param name="src" select="src"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:if test="not(translate(substring($name, string-length($name), 1), '12', '') = '')">
            <other><xsl:value-of select="src"/></other>
        </xsl:if>
        <xsl:apply-templates select="following-sibling::image[1]" mode="other"/>
    </xsl:template>
    
    <xsl:template name="find-name">
        <xsl:param name="src"/>
        <xsl:if test="contains($src, '_')">
            <xsl:value-of select="substring-before($src, '_')"/>
            <xsl:if test="contains(substring-after($src, '_'), '_')">
                <xsl:text>_</xsl:text>
            </xsl:if>
            <xsl:call-template name="find-name">
                <xsl:with-param name="src" select="substring-after($src, '_')"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    
    
</xsl:stylesheet>

Open in new window

0
 
tia_kamakshiAuthor Commented:
I wish to be your student. Don't know if it is possible

Works great.

Many Many thanks
0
 
Geert BormansCommented:
Welcome

Getting the image name is just a recursive template that stops at the last '_'

The tricky thing about getting the numbered images is that I walk the following-sibling axis starting at the first image. The main advantage doing it this way is that you only continue to the next node under certain conditions... so I can stop when the first is found, or the second. Differentiation is through modes.

(I have done Skype coaching in the past, not regular training, but working through exercises to learn. Works pretty well. There is a "hire me" button somewhere around here if you want to discuss that)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tia_kamakshiAuthor Commented:
Thanks Gertone

I really wanted to learn tricks you do.

How much your training costs. I am not finding any hire me button.

Please help me with it and I need to know the cost and how training will be done.

Thanks again
0
 
Geert BormansCommented:
If you check the "about me" on my profile there is a web reference leading to my coordinates
0
 
tia_kamakshiAuthor Commented:
Thanks Geert,

I got your details. I can see your next open training alredy happened in Dec 2011 and not seeing any details about future training

I will write you an email for next open training schedule

Regards,
0
 
Geert BormansCommented:
Oh, that is open training in Dutch (open as in any one can subscribe)
Only have done training for private people and on site for companies since
Let's discuss over email
0
 
tia_kamakshiAuthor Commented:
Hi Gertone,

I have sent you the email.

Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now