【问题标题】:Looping xml node in xslt在xslt中循环xml节点
【发布时间】:2016-10-21 12:08:07
【问题描述】:

我认为自己仍然是 xslt 的新手,因为到目前为止我所做的只是使用模板对象和变量进行基本操作并获得 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" /><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>

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

感谢您对此的任何帮助

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

【问题讨论】:

    标签: xslt


    【解决方案1】:

    这是一个完整的工作示例,如何做到这一点

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

    这里有一个(未指定的)源 XML 文档

    <body/>
    

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

    在这种情况下,XML 文档位于c:\temp\delete\products.xml

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

    当对(未指定的、单元素的)源 XML 文档应用转换时,会产生想要的结果

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

    更新

    使用嵌入样式表的 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" /><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>
    

    【讨论】:

    • 感谢您的解决方案。有没有一种方法可以将 xml 或 xml 作为字符串传递,然后在 xslt.xml 中将字符串转换为 xml。我想避免文件操作?
    • @Saj 这是可能的。我将在今天晚些时候为答案添加更新,因为我现在要去上班了
    • @Saj,我更新了答案,现在它包括使用嵌入式 XML 的替代转换。试试看。请注意,如果您的 XSLT 1.0 处理器没有实现 EXSLT,您需要使用特定于供应商的扩展命名空间 URI——这应该可以从 XSLT 处理器的文档中获得。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多