Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Duplicating main node

Hi Experts,

I am working on ASP.net 2.0 using C#

My xslt is producing root node (<articles>) again and again with each match result

Please find my xslt, xml, output below

XSLT:
<?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="LibraryCategory">Our Services</xsl:param>

  <xsl:template match="Image">
    <articles>
      <xsl:if test="@CategoryName=$LibraryCategory">
        <xsl:apply-templates select="." mode="pagination" />
      </xsl:if>
    </articles>
  </xsl:template>

  <xsl:template match="Image" mode="pagination">
    <article>
      <a href="/english/media/media-library/images/image01.aspx" data-lightbox="/english/media/media-library/images/image01.aspx?html=fragment">
        <img src="/images/media/image_thumbnail01.jpg" alt="" >
          <xsl:attribute name="src">
            <xsl:value-of select="PreviewImg"/>
          </xsl:attribute>
        </img>
      </a>
    </article>
  </xsl:template>

</xsl:stylesheet>

Open in new window


XML:
<?xml version="1.0"?>
<ImagesLibrary publicationId="tcm:0-526-1">  
    <Image tcmID="tcm:526-703372" orderno="1" CategoryName="Our Services" Title="Test 6" Published="false" UpdateDate="8/9/2011 1:55:00 PM" URL="/en/Images/ridesandslides_tcm526-702717.jpg">
      <PreviewImg Orientation="Portrait">/en/Images/aoa_tcm526-702718.png</PreviewImg>
      <LowRImg size="210 KB">/en/Images/mainbookingatour_tcm526-702719.jpg</LowRImg>
      <HighRImg size="366 KB">/en/Images/coast_main_tcm526-702720.jpg</HighRImg>
    </Image>
    <Image tcmID="tcm:526-702723" Title="Test1" CategoryName="Another Services" Published="true" UpdateDate="8/8/2011 1:54:00 PM" URL="/en/Images/ridesandslides_tcm526-702717.jpg">
      <PreviewImg Orientation="Portrait">/en/Images/aoa_tcm526-702718.png</PreviewImg>
      <LowRImg size="210 KB">/en/Images/mainbookingatour_tcm526-702719.jpg</LowRImg>
      <HighRImg size="366 KB">/en/Images/coast_main_tcm526-702720.jpg</HighRImg>
    </Image>
  <Image tcmID="tcm:526-702723" Title="Test1" CategoryName="Our Services" Published="true" UpdateDate="8/8/2011 1:54:00 PM" URL="/en/Images/ridesandslides_tcm526-702717.jpg">
    <PreviewImg Orientation="Portrait">/en/Images/aoa_tcm526-702718.png</PreviewImg>
    <LowRImg size="210 KB">/en/Images/mainbookingatour_tcm526-702719.jpg</LowRImg>
    <HighRImg size="366 KB">/en/Images/coast_main_tcm526-702720.jpg</HighRImg>
  </Image>
  
</ImagesLibrary>

Open in new window


OUTPUT
<?xml version="1.0" encoding="utf-8"?>
  <articles><article><a href="/english/media/media-library/images/image01.aspx" data-lightbox="/english/media/media-library/images/image01.aspx?html=fragment"><img alt="" src="/en/Images/aoa_tcm526-702718.png" /></a></article></articles>
  <articles />
  <articles><article><a href="/english/media/media-library/images/image01.aspx" data-lightbox="/english/media/media-library/images/image01.aspx?html=fragment"><img alt="" src="/en/Images/aoa_tcm526-702718.png" /></a></article></articles>

Open in new window




EXPECTED OUTPUT
<?xml version="1.0" encoding="utf-8"?>
<articles>
  <article>
    <a href="/english/media/media-library/images/image01.aspx" data-lightbox="/english/media/media-library/images/image01.aspx?html=fragment">
      <img alt="" src="/en/Images/aoa_tcm526-702718.png" />
    </a>
  </article>

  <article>
    <a href="/english/media/media-library/images/image01.aspx" data-lightbox="/english/media/media-library/images/image01.aspx?html=fragment">
      <img alt="" src="/en/Images/aoa_tcm526-702718.png" />
    </a>
  </article>
</articles>

Open in new window


Also, I donot wanted
<?xml version="1.0" encoding="utf-8"?>
in my output

Please help me fixing the issue

Thanks
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

<xsl:template match="Image">
    <articles>
      <xsl:if test="@CategoryName=$LibraryCategory">
        <xsl:apply-templates select="." mode="pagination" />
      </xsl:if>
    </articles>
  </xsl:template>

Open in new window


should be

  <xsl:template match="ImagesLibrary ">
    <articles>
        <xsl:apply-templates />
    </articles>
  </xsl:template>
  <xsl:template match="Image">
        <xsl:apply-templates select="self::*[="@CategoryName=$LibraryCategory]" mode="pagination" />
  </xsl:template>

Open in new window

this way you create the articles node one level higher
SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but your full xslt looks nicer like this (no duplicate processing but precise selection



<?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="LibraryCategory">Our Services</xsl:param>
    
    <xsl:template match="ImagesLibrary">
        <articles>
            <xsl:apply-templates select="Image[@CategoryName=$LibraryCategory]"/>
        </articles>
    </xsl:template>
    
    <xsl:template match="Image">
        <article>
            <a href="/english/media/media-library/images/image01.aspx" data-lightbox="/english/media/media-library/images/image01.aspx?html=fragment">
                <img src="/images/media/image_thumbnail01.jpg" alt="" >
                    <xsl:attribute name="src">
                        <xsl:value-of select="PreviewImg"/>
                    </xsl:attribute>
                </img>
            </a>
        </article>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

changing the output line in the xslt to omit the declaration will get rid of teh declaration

    <xsl:output indent="yes" omit-xml-declaration="yes"/>

but I assume your C# will ignore the serialisation settings.
If the above does not work, please show us the transforming C#
Avatar of tia_kamakshi

ASKER

Many Thanks for your replies

Can I remove the line below from my output
<?xml version="1.0" encoding="utf-8"?>

Also, can I add condition here saying if
value of parameter is 'All' or '' then give results from all image nodes

somethhing like
<xsl:apply-templates select="Image"/>

if parameter LibraryCategory has the value other than 'All' or ''
then give results from
<xsl:apply-templates select="Image[@CategoryName=$LibraryCategory]"/>

Thanks again
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many Many Thanks