1

I consider myself still a newbie to xslt as all I did so far is basic opertion using templateobject and variables and get an html output. I am into my learning step for complex computation. I need help from the experts in the forum to help resolve one of my situation.

I am building template for email. Below is my xslt that I want to transform. Additional to this I want to pass another xml to it for the xslt to loop through and get the attribute value to assign in the respective places in the html.

Below code doesn't work, just an example to demonstrate on what I intended to do.

XSLT

  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:templateObject="urn:data">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:variable name="products" />
  <xsl:variable name="doc" select="document($products)"/>
  <xsl:template match="/body">
  <html>
  <body bgcolor="#E5E5E5" leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
  <table style="width:100%;" class="orderItems">
                        <xsl:for-each select="$doc">
                     <tbody>
                     <tr>
                        <td class="itemThumbnail" style="width:80px; height:70px; padding-top:20px;">
                          <img src="ref" alt="" />
                        </td>
                        <td style="padding-top:20px; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
                        <xsl:value-of select="$ProductName" /><br />
                          Quantity: 1
                        </td>
                        <td style="text-align:right; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
                          Price: <strong style="font-size:20px; color:#b9277e;">
                            <xsl:value-of select="$Amount" />
                          </strong>
                        </td>
                      </tr>
                      </tbody>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

XML

<Root>
  <item ProductName="abc" Amount="$20" />
  <item ProductName="xyz" Amount="$50" />
</Root>

I tried to assign the xml as string to the xslt variable and tried to create a document with in XSLT using document() function but still not able to loop through the element and attributes.

Appreciate any help on this

Couple of other queries: - Can I nest xsl:template by assigning multiple xml to xslt through c# by declaring the namespace on the header? - Can conversion be possible in xslt from string to xml?

0

1 Answer 1

1

Here is a complete, working example how to do this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" omit-xml-declaration="yes" indent="yes" />
 <xsl:strip-space elements="*"/>
 <xsl:param name="pPath" select="'file:///c:/temp/delete/products.xml'"/>

 <xsl:variable name="vdocProducts" select="document($pPath)"/>

  <xsl:template match="/body">
        <html>
          <body bgcolor="#E5E5E5" leftmargin="0" marginwidth="0" topmargin="0" 
                    marginheight="0" offset="0">
          <table style="width:100%;" class="orderItems">
            <xsl:apply-templates select="$vdocProducts/*/item"/>
          </table>
         </body>
        </html>
  </xsl:template>

  <xsl:template match="item">
      <tbody>
        <tr>
          <td class="itemThumbnail" style="width:80px; height:70px; padding-top:20px;">
           <img src="ref" alt="" />
          </td>
          <td style="padding-top:20px; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
            <xsl:value-of select="@ProductName" /><br />
             Quantity: 1
          </td>
          <td style="text-align:right; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
            Price: <strong style="font-size:20px; color:#b9277e;">
                            <xsl:value-of select="@Amount" />
                    </strong>
          </td>
    </tr>
  </tbody>
  </xsl:template>
</xsl:stylesheet>

Here we have a (unspecified) source XML document:

<body/>

and the provided XML document is residing in the filesystem, its file URI being the value of the global parameter $pPath.

In this case the XML document resides at c:\temp\delete\products.xml :

<Root>
  <item ProductName="abc" Amount="$20" />
  <item ProductName="xyz" Amount="$50" />
</Root>

When the transformation is applied against the (unspecified, single-element) source XML document, the wanted result is produced:

<html>
   <body bgcolor="#E5E5E5" leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
      <table style="width:100%;" class="orderItems">
         <tbody>
            <tr>
               <td class="itemThumbnail" style="width:80px; height:70px; padding-top:20px;"><img src="ref" alt=""></td>
               <td style="padding-top:20px; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">abc<br>
                  Quantity: 1

               </td>
               <td style="text-align:right; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
                  Price: <strong style="font-size:20px; color:#b9277e;">$20</strong></td>
            </tr>
         </tbody>
         <tbody>
            <tr>
               <td class="itemThumbnail" style="width:80px; height:70px; padding-top:20px;"><img src="ref" alt=""></td>
               <td style="padding-top:20px; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">xyz<br>
                  Quantity: 1

               </td>
               <td style="text-align:right; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
                  Price: <strong style="font-size:20px; color:#b9277e;">$50</strong></td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

Update:

Using embedded in the stylesheet XML tree:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" xmlns:my="my:my" exclude-result-prefixes="my ext">
 <xsl:output method="html" omit-xml-declaration="yes" indent="yes" />
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vrtfdocProducts">
    <Root>
      <item ProductName="abc" Amount="$20" />
      <item ProductName="xyz" Amount="$50" />
    </Root>
 </xsl:variable>
 <xsl:variable name="vdocProducts" select="ext:node-set($vrtfdocProducts)"/>

  <xsl:template match="/body">
        <html>
          <body bgcolor="#E5E5E5" leftmargin="0" marginwidth="0" topmargin="0" 
                    marginheight="0" offset="0">
          <table style="width:100%;" class="orderItems">
            <xsl:apply-templates select="$vdocProducts/*/item"/>
          </table>
         </body>
        </html>
  </xsl:template>

  <xsl:template match="item">
      <tbody>
        <tr>
          <td class="itemThumbnail" style="width:80px; height:70px; padding-top:20px;">
           <img src="ref" alt="" />
          </td>
          <td style="padding-top:20px; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
            <xsl:value-of select="@ProductName" /><br />
             Quantity: 1
          </td>
          <td style="text-align:right; font-family:Arial, Verdana, Helvetica, sans-serif; font-size: 16px; line-height: 20px; color:#4c4c4c;">
            Price: <strong style="font-size:20px; color:#b9277e;">
                            <xsl:value-of select="@Amount" />
                    </strong>
          </td>
    </tr>
  </tbody>
  </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the solution. Is there a way that I can pass xml or xml as string and then convert string to xml in the xslt. I wanted to avoid file operation?
@Saj this is possible. I will add an update to the answer, later today, as I'm now heading for work
@Saj, I updated the answer and it now includes an alternative transformation that uses embedded XML. Try it. Do note that if your XSLT 1.0 processor doesn't implement EXSLT, you need to use your vendor-specific extension namespace URI -- this should be available from the documentation of the XSLT processor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.