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

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
0
tia_kamakshi
Asked:
tia_kamakshi
  • 6
  • 2
2 Solutions
 
Geert BormansCommented:
<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

0
 
Geert BormansCommented:
this way you create the articles node one level higher
0
 
zc2Commented:
Each time the "Image" template is applied it adds an extra "articles" element. So I made an additional upper level template which applies to the "ImagesLibrary" element. It adds the "articles" to the output and applies all the images.
Also, note, I got rid of the previous solution, moving the @CategoryName=$LibraryCategory filter to the apply-template statement. And I don't know why you need two different Image template, but I removed one.

<?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]" mode="pagination"/>
		</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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Geert BormansCommented:
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

0
 
Geert BormansCommented:
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#
0
 
tia_kamakshiAuthor Commented:
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
0
 
Geert BormansCommented:
first item has been dealt with, see remark on xsl:output

second replace the ImagesLibrary template in my last proposed solution like this

    <xsl:template match="ImagesLibrary">
        <articles>
            <xsl:apply-templates select="Image[@CategoryName=$LibraryCategory or $LibraryCategory = 'All']"/>
        </articles>
    </xsl:template>

Open in new window

0
 
tia_kamakshiAuthor Commented:
Many Many Thanks
0
 
Geert BormansCommented:
welcome
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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