?
Solved

Extracting the details from 2 xmls and seperating the output

Posted on 2012-08-29
5
Medium Priority
?
436 Views
Last Modified: 2012-09-02
Hi Experts,

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

I have to bring data from 2 xmls and bring the output.

I have my summary xml below. For each article in summary xml below, there is a detail xml. Path of the detail xml is mentioned in each xml attribute such as

xml="News\English\2012\944714.xml"

So, detail of article id=944714 is present in the xml at path:
News\English\2012\944714.xml

Yes, we need to add the root path of the xml from variable or param we will be mentioning above, so the complete path of the file will be

path in parameter + News\English\2012\944714.xml
result may be
\\servername\newsfolder\News\English\2012\944714.xml

Here is my summary xml below
<?xml version="1.0" encoding="utf-16"?>
<newsindex basePath="//servername2/www$/news/">
  <imagepath>http://servername/newsimages</imagepath>

  <article id="944714" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title1</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.1</summary>
  </article>
  <article id="944718" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title2</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.2</summary>
  </article>
  <article id="944314" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title3</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.3</summary>
  </article>
  <article id="965714" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title4</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.4</summary>
  </article>
  <article id="948714" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title5</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.5</summary>
  </article>
  <article id="944214" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title6</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.6</summary>
  </article>
  <article id="944784" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title7</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.7</summary>
  </article>

  <article id="944784" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title8</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.7</summary>
  </article>

  <article id="944784" xml="News\English\2012\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
    <title>Article Title9</title>
    <summary>Lorem ipsum dolor sit amet, philomusia in deinde vero non ait mea in modo genito in.7</summary>
  </article>
</newsindex>

Open in new window


Here is the detail xml corresponding to each article id. Each article ID has seperate xml as below
Xml for article having id=944714
<?xml version="1.0" encoding="utf-8" ?>
<news type="detail">
  <article id="944714">
    <images>
      <image id="757233" type="M">
        <src><![CDATA[image01_tcm133-757233.jpg]]></src>
      </image>
      <image id="757231" type="M">
        <src><![CDATA[abc02_tcm133-757231.jpg]]></src>
      </image>
    </images>
  </article>
</news>

Xml for article having id=944718
<?xml version="1.0" encoding="utf-8" ?>
<news type="detail">
  <article id="944718">
    <images>
      <image id="757232" type="M">
        <src><![CDATA[imaertge1_tcm133-7572433.jpg]]></src>
      </image>
      <image id="757241" type="M">
        <src><![CDATA[abafgc2_tcm133-757231.jpg]]></src>
      </image>
    </images>
  </article>
</news>

Open in new window


I need the output as follows:
First 4 article image to be picked from detail article xml.
Where image name has 2 in last before last underscore "_"
<div class="fulls">
  abc02_tcm133-757231.jpg
  abafgc2_tcm133-757231.jpg
  Image name with ending 2 from article 3
  Image name with ending 2 from article 4
</div>

First 4 article image to be picked from detail article xml.
Where image name has 1 in last before last underscore "_

<div class="thumbnails">
  image01_tcm133-757233.jpg
  imaertge1_tcm133-7572433.jpg

  Image name with ending 1 from article 3
  Image name with ending 1 from article 4
</div>

First 4 article title from summary node
<div class="text_left">
  Article Title1
  Article Title2
  Article Title3
  Article Title4
</div>

Next 3 article as below
<div class="text_right">
  Article Title5
  Article Title6
  Article Title7
</div>

Open in new window


So, the final output will be as follows:
<div class="fulls">
  abc02_tcm133-757231.jpg
  abafgc2_tcm133-757231.jpg
  Image name with ending 2 from article 3
  Image name with ending 2 from article 4
</div>

<div class="thumbnails">
  image01_tcm133-757233.jpg
  imaertge1_tcm133-7572433.jpg

  Image name with ending 1 from article 3
  Image name with ending 1 from article 4
</div>

<div class="text_left">
  Article Title1
  Article Title2
  Article Title3
  Article Title4
</div>

<div class="text_right">
  Article Title5
  Article Title6
  Article Title7
</div>

Open in new window


Extracting the images from detail page logic is already provided at:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_27845061.html#a38342200

So, we will be executing our xslt on summary xml from there we will find the details of detail xml.

Please help me with the solution from 2 documents



Many Thanks
0
Comment
Question by:tia_kamakshi
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 38349172
I don't have the time to provide a full solution today,
but you can build it yourself, based on this example and the image processing code

<?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:param name="base">file:///C:/Users/User/Desktop/</xsl:param>
    <xsl:template match="newsindex">
        <div class="fulls">
            <xsl:for-each select="article[position() &lt;= 4]">
                <xsl:variable name="url" select="concat($base, @xml)"/>
                <xsl:apply-templates select="(document($url)//image)[1]" mode="second"></xsl:apply-templates>
            </xsl:for-each>
        </div>
    </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"/><br/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="following-sibling::image[1]" mode="second"/>
            </xsl:otherwise>
        </xsl:choose>
    </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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38349182
you need to pass in a parameter $base containing the base path

this solution uses the document() function, so you need to enable that in your C#
(if not you can't load a second or third... document)
look here
http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltsettings.aspx
property: EnableDocumentFunction
0
 

Author Comment

by:tia_kamakshi
ID: 38354435
Thanks Gertone,

It works great. I have completed the xslt. One thing I donot understood that above xslt is produicing the results even without below code. Which is extracting images with 1 in last.

<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>

Open in new window


When I am writting above code and calling template like
<div class="thumbnails">
      <xsl:for-each select="article[position() &lt;= 4]">
        <xsl:variable name="url" select="concat($base, @xml)"/>
        <xsl:apply-templates select="(document($url)//image)[1]" mode="first"/>
      </xsl:for-each>
    </div>

Open in new window


Then it is producing results from this template. How image name with 1 in last is extarcting without above mentioned code in this post

Thanks for your great help
0
 

Author Closing Comment

by:tia_kamakshi
ID: 38356647
Many Thanks for your help
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38358716
welcome,

I must admit that I don't understand your follow up question.
I think I need to view the full XSLT in order to understand
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question