XSL1.0生成Ascending Seq#

时间:2013-05-19 14:30:00

标签: xslt

我正在使用XSL1.0。我的编辑器/调试器是带有Saxon的OxygenXML(OxygenXML无法使用MSXML进行调试),它将部署为仅使用MSXML的第三方应用程序。这意味着如果我想能够调试,我就不能使用包含节点集的变量。

问题可能可能表示为如何按顺序编号输出以下内容 -

<xsl:for-each select="node1">
   <xsl:variable name="current_ID" select="ID">
     <xsl:for-each select="sub_node1">
       <xsl:value-of select="../ID"/>-<xsl:value-of select="Sub_ID"/>
     </xsl:for-each>
 </xsl:for-each>

理解我不能简单地在我的场景中使用它:

 <xsl:for-each select="node1/sub_node1">
       <xsl:value-of select="position()"/>
    </xsl:for-each>

下面是一个制作的示例,它显示了我试图解决的问题,作为更大的XSL / XML组合的一部分。我基本上需要创建制造说明。除产品/版本(按数量)外,XML中的所有节点都处于正确的顺序,我无法更改它。我需要从3个不同的XSL生成相同的序列号。我目前的上下文将始终是shipping / deliveryies / delivery_products(即我的XSL必须处理所示seq中的节点)。我需要生成按版本数量和交货顺序排序的产品列表。每行在下面的例子中应该有一个连续的no(1-4)

<shipments>
<product>
    <name>Product 1</name>
    <prod_id>P1</prod_id>
    <version>
        <version_id>P1_V1</version_id>
        <qty>8800</qty>
    </version>
    <version>
        <version_id>P1_V2</version_id>
        <qty>1100</qty>
    </version>
    <version>
        <version_id>P1_V3</version_id>
        <qty>100</qty>
    </version>
</product>
<product>
    <name>Product 2</name>
    <prod_id>P2</prod_id>
    <version>
        <version_id>P2_V1</version_id>
        <qty>5000</qty>
    </version>
    <version>
        <version_id>P2_V2</version_id>
        <qty>5000</qty>
    </version>
    <version>
        <version_id>P2_V3</version_id>
        <qty>2000</qty>
    </version>
</product>
<deliveries>
    <del_id>1</del_id>
    <destination>Miami</destination>
    <delivery_products>
        <version_id>P1_V1</version_id>
        <qty>8000</qty>
    </delivery_products>
    <delivery_products>
        <version_id>P2_V1</version_id>
        <qty>5000</qty>
    </delivery_products>
</deliveries>
<deliveries>
    <del_id>2</del_id>
    <destination>New York</destination>
    <delivery_products>
        <version_id>P1_V1</version_id>
        <qty>800</qty>
    </delivery_products>
    <delivery_products>
        <version_id>P2_V2</version_id>
        <qty>1000</qty>
    </delivery_products>
</deliveries>

预期产量低于。注意seq#从1开始,最多4个

    <table>
 <thead>
    <tr>
       <td class="col_head">
          Seq  
       </td>
       <td class="col_head">
          Version
       </td>
       <td class="col_head">
          Destination
       </td>
       <td class="col_head">
          Qty
       </td>
    </tr>
 </thead>
 <tr>
    <td colspan="4" class="rev_heading">Product 1</td>
 </tr>
 <tr>
    <td>1</td>
    <td>P1_V1</td>
    <td>Miami</td>
    <td>8000</td>
 </tr>
 <tr>
    <td>2</td>
    <td>P1_V1</td>
    <td>New York</td>
    <td>800</td>
 </tr>
 <tr>
    <td colspan="4" class="rev_heading">Product 2</td>
 </tr>
 <tr>
    <td>3</td>
    <td>P2_V1</td>
    <td>Miami</td>
    <td>5000</td>
 </tr>
 <tr>
    <td>4</td>
    <td>P2_V2</td>
    <td>New York</td>
    <td>5000</td>
 </tr>
</table>

到目前为止,这是我的XSL(只是将一个位置()插入到seq的占位符#)

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

   <xsl:template match="shipments">    
    <html>
      <head>
        <title>Seq Test</title>

        <style type="text/css">
        table {border: 1px solid black; border-collapse: collapse;}
        td {border: 1px solid black; padding: 1px 5px 1px 5px;}
        .col_head {font-weight: 600;}
        .rev_heading {color: red; text-align: center; padding-top: 15px;}
       </style>
      </head>
      <body>

       <table>
          <thead>
            <tr>
              <!-- SEQ# -->
              <td class="col_head">
                Seq
              </td>
              <!-- Imprint/Version -->
              <td class="col_head">
                Version
              </td>
              <!-- Ship to -->
              <td class="col_head">
                Destination
              </td>
              <!-- Qty -->
              <td class="col_head">
                Qty
              </td>
            </tr>
          </thead>
          <xsl:for-each select="product">
            <xsl:sort data-type="number" select="qty"/>
              <xsl:for-each select="version">
                <xsl:variable name="curr_version" select="version_id"/>
                <xsl:if test="position() = 1"> 
                  <tr>
                    <td colspan="4" class="rev_heading">                                
                        <xsl:value-of select="../name"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:for-each select="../../deliveries/delivery_products[version_id = $curr_version]">
                  <tr >
                    <!-- SEQ# -->
                    <td>
                      <xsl:value-of select="position()"/>
                    </td>
                    <!-- Version -->
                    <td>
                      <xsl:value-of select="version_id"/>
                    </td>
                    <!-- Ship to -->
                    <td>
                      <xsl:value-of select="../destination"/>
                    </td>
                    <!-- QTY -->
                    <td>
                      <xsl:value-of select="qty"/>
                    </td>
                  </tr>
              </xsl:for-each> 
              </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

  

我正在使用XSL1.0。我的编辑器/调试器是带有Saxon的OxygenXML   (OxygenXML无法使用MSXML进行调试),它将部署为使用   仅使用MSXML的第三方应用。这意味着我不能使用变量   如果我想能够调试,则包含一个节点集。

您仍然可以使用oXygen和EXSLT node-set()函数

完成后,只需将名称空间-uri从"http://exslt.org/common"更改为"urn:schemas-microsoft-com:xslt"

以下是此技术的简短示例。假设您已完成以下转换的调试:

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

 <xsl:template match="/*">
   <xsl:variable name="vrtfPass1">
         <xsl:apply-templates select="num[. mod 3 = 0]"/>
   </xsl:variable>

   <xsl:copy-of select="sum(ext:node-set($vrtfPass1)/*)"/>
 </xsl:template>

 <xsl:template match="num">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

然后从EXSLT namespace-uri更改为MSXSL名称空间uri:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="urn:schemas-microsoft-com:xslt">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
   <xsl:variable name="vrtfPass1">
         <xsl:apply-templates select="num[. mod 3 = 0]"/>
   </xsl:variable>

   <xsl:copy-of select="sum(ext:node-set($vrtfPass1)/*)"/>
 </xsl:template>

 <xsl:template match="num">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

最后,使用MSXML运行最后一次转换,它产生的结果与使用EXSLT的初始转换完全相同

18
相关问题