Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xslt中的循环xml节点 我认为自己仍然是XSLT的新手,因为到目前为止我所做的都是使用TeMeTeLabor和变量的基本操作,并得到HTML输出。我正在学习复杂的计算。我需要论坛专家的帮助来解决我的一个问题_Xslt - Fatal编程技术网

xslt中的循环xml节点 我认为自己仍然是XSLT的新手,因为到目前为止我所做的都是使用TeMeTeLabor和变量的基本操作,并得到HTML输出。我正在学习复杂的计算。我需要论坛专家的帮助来解决我的一个问题

xslt中的循环xml节点 我认为自己仍然是XSLT的新手,因为到目前为止我所做的都是使用TeMeTeLabor和变量的基本操作,并得到HTML输出。我正在学习复杂的计算。我需要论坛专家的帮助来解决我的一个问题,xslt,Xslt,我正在为电子邮件建立模板。下面是我想要转换的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 metho

我正在为电子邮件建立模板。下面是我想要转换的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>


数量:1 价格:
XML

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

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

谢谢你在这方面的帮助

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

下面是一个完整的工作示例,说明如何执行此操作:

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

abc
数量:1 价格:20美元 xyz
数量:1 价格:50美元

更新

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


数量:1 价格:
感谢您提供的解决方案。是否有一种方法可以将xml或xml作为字符串传递,然后在xslt中将字符串转换为xml。我想避免文件操作?@Saj这是可能的。今天晚些时候,我将为答案添加一个更新,因为我现在正朝着这个方向前进work@Saj,我更新了答案,现在它包含了一个使用嵌入式XML的替代转换。试试看。请注意,如果XSLT1.0处理器没有实现EXSLT,则需要使用特定于供应商的扩展命名空间URI——这应该可以从XSLT处理器的文档中获得。