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

xslt is producing unwanted output

Hi Experts,

I am working on ASP.net 2.0 application using C#

My xslt is producing unwanted output.

Below is my 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:template match="Image[@CategoryName='Our Services']">
      <xsl:apply-templates select="." mode="pagination" />
  </xsl:template>
  
  <xsl:template match="Image" mode="pagination">
    <article>
      <xsl:value-of select="PreviewImg"/>
    </article>
  </xsl:template>
</xsl:stylesheet>

Open in new window


and Below is my 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 coming as follows:
<?xml version="1.0" encoding="utf-8"?>  
    <article>/en/Images/aoa_tcm526-702718.png</article>
    
      /en/Images/aoa_tcm526-702718.png
      /en/Images/mainbookingatour_tcm526-702719.jpg
      /en/Images/coast_main_tcm526-702720.jpg
    
  <article>/en/Images/aoa_tcm526-702718.png</article>
  

Open in new window

   

Expected output
<article>/en/Images/aoa_tcm526-702718.png</article>
  <article>/en/Images/aoa_tcm526-702718.png</article>

Open in new window

       
Why in output it is comming extra content
/en/Images/aoa_tcm526-702718.png
            /en/Images/mainbookingatour_tcm526-702719.jpg
      /en/Images/coast_main_tcm526-702720.jpg

Open in new window


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

Please suggest
0
tia_kamakshi
Asked:
tia_kamakshi
  • 3
  • 2
1 Solution
 
zc2Commented:
XSLT's "default template" processes all other elements which your template did not process. Just add this empty template to ignore them:
  <xsl:template match="Image[@CategoryName!='Our Services']">
  </xsl:template>

Open in new window

0
 
tia_kamakshiAuthor Commented:
Thanks for your reply

I tried but this does not helped. There was no change in the result output after adding template as below

  <xsl:template match="Image[@CategoryName!='Our Services']">
  </xsl:template>
 
  In my actual requirenment this categoryname has to come from parameter. I have added param in my 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[@CategoryName=$LibraryCategory]">
      <xsl:apply-templates select="." mode="pagination" />
  </xsl:template>
  
  <xsl:template match="Image" mode="pagination">
    <article>
      <xsl:value-of select="PreviewImg"/>
    </article>
  </xsl:template>
  <xsl:template match="Image[@CategoryName!=$LibraryCategory]">
  </xsl:template>
</xsl:stylesheet>

Open in new window


If parameter LibraryCategory value is blank or All then I wanted to bring all results irrespective of CategoryName

My above xslt is now giving error at line
<xsl:template match="Image[@CategoryName=$LibraryCategory]">

error: Variables cannot be used within this expression.

Please help me with xslt
0
 
zc2Commented:
So, that means, a template param can not be a part of a template pattern. Here's a workaround:

<?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">
  	<xsl:if test="@CategoryName=$LibraryCategory">
  		<xsl:apply-templates select="." mode="pagination" />
   </xsl:if>
  </xsl:template>
  
  <xsl:template match="Image" mode="pagination">
    <article>
      <xsl:value-of select="PreviewImg"/>
    </article>
  </xsl:template>
 
</xsl:stylesheet>

Open in new window

0
 
tia_kamakshiAuthor Commented:
Many Many Thanks for your help
0
 
zc2Commented:
You're welcome
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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