首页 文章

在xslt中循环xml节点

提问于
浏览
1

我认为自己仍然是xslt的新手,因为到目前为止我所做的只是使用templateobject和变量的基本操作并得到一个html输出 . 我正在进行复杂计算的学习步骤 . 我需要论坛专家帮助解决我的一个问题 .

我正在为电子邮件构建模板 . 下面是我要转换的xslt . 除此之外,我想将另一个xml传递给它,让xslt循环并获取要在html中各自位置分配的属性值 .

下面的代码不起作用,只是一个示例来说明我打算做什么 .

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

我尝试将xml作为字符串分配给xslt变量,并尝试使用document()函数在XSLT中创建一个文档,但仍然无法遍历元素和属性 .

感谢任何帮助

几个其他查询: - 我可以通过在 Headers 上声明命名空间,通过c#为xslt分配多个xml来嵌套xsl:template吗? - 可以在xslt中从字符串转换为xml吗?

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

    并且提供的XML文档驻留在文件系统中,其文件URI是全局参数 $pPath 的值 .

    在这种情况下,XML文档位于 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

    使用样式表XML树中的嵌入:

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

相关问题