【问题标题】:xslt 1.0 adding line between groupsxslt 1.0在组之间添加行
【发布时间】:2014-04-25 20:10:02
【问题描述】:

我在转换此 XML 时遇到问题

<?xml version="1.0"?>
<Report username="TONYB" sessionId="20140425154921">
  <Result>
    <Request Type="InventoryShip">
      .... Omitted for brevity
    </Request>
    <Information>
      .... Omitted for brevity
    </Information>
    <Inventory>
      <Sku>PBM118-00</Sku>
      <RevisionNo/>
      <OnHand>34010</OnHand>
      .... Omitted for brevity
    </Inventory>
   <Inventory>
      <Sku>PFC00345</Sku>
      <RevisionNo/>
      <OnHand>0</OnHand>
      .... Omitted for brevity
    </Inventory>
    <Inventory>
      <Sku>PFC00476</Sku>
      <RevisionNo/>
      <OnHand>2025</OnHand>
      .... Omitted for brevity
    </Inventory>
    <Item>
      <Sku>PBM118-00</Sku>
      <CustomerPart>CP-0004</CustomerPart>
      <Description>PACKING SLIP GE-M CHANGED XX WITH A LONG DESC</Description>
      <Groups>
        <Group>BOOK</Group>
        .... Omitted for brevity
        <Group>HR</Group>
        .... Omitted for brevity
      </Groups>
      .... Omitted for brevity
      <LowestUOM>EA</LowestUOM>
    </Item>
    <Item>
      <Sku>PFC00345</Sku>
      <CustomerPart>1001</CustomerPart>
      <Description>item description 8/29/2011 16:06</Description>
      <Groups>
        <Group>F60</Group>
        <Group>FAKE2</Group>
        <Group>HR</Group>
        <Group>TYPE</Group>
      </Groups>
      .... Omitted for brevity
      <LowestUOM>EA</LowestUOM>
    </Item>
    <Item>
      <Sku>PFC00476</Sku>
      <CustomerPart>PBM119-88</CustomerPart>
      <Description>PBM118-99 NEW ITEM</Description>
      <Groups>
        <Group>BOOK</Group>
        <Group>HR</Group>
      </Groups>
      .... Omitted for brevity
      <LowestUOM>EA</LowestUOM>
    </Item>
    <Usage>
      .... Omitted for brevity
    </Usage>
    <Usage>
      <DateRange>
        <Start>01/01/2014</Start>
        <End>12/31/2014</End>
      </DateRange>
      <OrderedBySku>
        <Sku>PBM118-00</Sku>
        <ShippedQty>951</ShippedQty>
        <User/>
      </OrderedBySku>
      <OrderedBySku>
        <Sku>PFC00476</Sku>
        <ShippedQty>0</ShippedQty>
        <User/>
      </OrderedBySku>
    </Usage>
    <Usage>
      <DateRange>
        <End>12/31/2014</End>
      </DateRange>
      <OrderedBySku>
        <Sku>PFC00476</Sku>
        <ShippedQty>0</ShippedQty>
        <User/>
      </OrderedBySku>
      <OrderedBySku>
        <Sku>PBM118-00</Sku>
        <ShippedQty>116668</ShippedQty>
        <User/>
      </OrderedBySku>
    </Usage>
  </Result>
</Report>

变成这样,在第一个“组”字段的每个转换之间有一个空行:

<html>
..... Omitted for brevity
<tbody>
<tr class="">
<td><img src="/wmsImages/SALIX/products/PBM118-00_THM.jpg"></td><td>CP-0004</td><td>PACKING SLIP GE-M CHANGED XX WITH A LONG DESC</td><td>U</td><td>Y</td><td class="number">34,010EA</td><td class="number">116,668</td><td class="number">100CA</td><td>BOOK</td><td>CORP</td><td>FAKE</td><td>FAKE2</td><td>FAKE3</td><td>FAKEDEPT</td><td>HR</td><td>REPORT</td><td>fake</td><td></td><td></td>
</tr>
<tr class="">
<td></td><td>PBM119-88</td><td>PBM118-99 NEW ITEM</td><td>U</td><td>Y</td><td class="number">2,025EA</td><td class="number">0</td><td class="number">EA</td><td>BOOK</td><td>HR</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td>1001</td><td>item description 8/29/2011 16:06</td><td>U</td><td>Y</td><td class="number">0EA</td><td class="number">0</td><td class="number">10RL</td><td>F60</td><td>FAKE2</td><td>HR</td><td>TYPE</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</tbody>
..... Omitted for brevity
</html>

我不断收到这个:

<html>
..... Omitted for brevity
<tbody>
<tr class="">
<td><img src="/wmsImages/SALIX/products/PBM118-00_THM.jpg"></td><td>CP-0004</td><td>PACKING SLIP GE-M CHANGED XX WITH A LONG DESC</td><td>U</td><td>Y</td><td class="number">34,010EA</td><td class="number">116,668</td><td class="number">100CA</td><td>BOOK</td><td>CORP</td><td>FAKE</td><td>FAKE2</td><td>FAKE3</td><td>FAKEDEPT</td><td>HR</td><td>REPORT</td><td>fake</td><td></td><td></td>
</tr>
<tr class="">
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td>PBM119-88</td><td>PBM118-99 NEW ITEM</td><td>U</td><td>Y</td><td class="number">2,025EA</td><td class="number">0</td><td class="number">EA</td><td>BOOK</td><td>HR</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td>1001</td><td>item description 8/29/2011 16:06</td><td>U</td><td>Y</td><td class="number">0EA</td><td class="number">0</td><td class="number">10RL</td><td>F60</td><td>FAKE2</td><td>HR</td><td>TYPE</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr class="">
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</tbody>
..... Omitted for brevity
</html>

那我做错了什么。是的,“组”标签是可选的,我们可以有 0 个或更多。但在这种情况下,如您所见,所有 3 个项目的第一个组列中都有内容。

这是我的 xslt:

<?xml version='1.0' encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import   href="report_common.xsl"/>
  <xsl:param    name="csvLink"                      />
  <xsl:param    name="thumbs"                       />
  <xsl:param    name="showOutOfStock" select="'Yes'"/>

  <xsl:variable name="xxshowRevision" select="'Junk'"           />
  <xsl:variable name="ThumbNames"     select="document($thumbs)"/>
  <xsl:variable name="maxGroupCols"   select="10"/>



  <!-- Define a rule to suppress header elements. -->
  <xsl:template mode="header"
      match="BelowLowPoint|Discontinued|Inactive|OrderStatus|Sku|DateRange">
  </xsl:template>

  <!-- Define the root template for this report. -->
  <xsl:template match="/Report">
<html>
  <head>
    <title>Inventory with Shipping Data</title>
    <meta http-equiv="Content-Type"
        content="text/html"/>
    <link href="/css/layout.css"
        rel="stylesheet" type="text/css" />
    <link href="/css/markup.css"
        rel="stylesheet" type="text/css" />
    <link href="/css/printer.css"
        rel="stylesheet" type="text/css" media='printer' />
    <link href="/css/SALIX.css"
        rel="stylesheet" type="text/css" />
    <style type="text/css">
.OutOfStock{
    background-color: red
}
    </style>
  </head>
  <body>
    <form action="">
      <input type="button" class="cssbutton no-print"
          onClick="window.print()"
          value="Print Screen"/>
    </form>
    <xsl:apply-templates select="Result"/>
    <div class="no-print">
      <xsl:element name="form">
        <xsl:attribute name="name"  >csvForm</xsl:attribute>
        <xsl:attribute name="method">post</xsl:attribute>
        <xsl:attribute name="action">
          <xsl:value-of select="concat('/staticreports/',$csvLink)"/>
        </xsl:attribute>
        <input type="hidden" name="reportType"
            value="StaticReport"/>

        <xsl:element name="input">
          <xsl:attribute name="type">hidden</xsl:attribute>
          <xsl:attribute name="name">reportTitle</xsl:attribute>
          <xsl:attribute name="value">
            <xsl:value-of select="$reportTitle"/>
          </xsl:attribute>

        </xsl:element>
        <a href="#"
            onClick="document.csvForm.submit();return false"
          >Click here to export into spreadsheet.</a>
      </xsl:element>
    </div>
  </body>
</html>
  </xsl:template>

  <xsl:key name="byGroup" match="Inventory" use="../Item/Sku[.=current()/Sku]/../Groups/Group[1]" />

  <xsl:template match="Result">
    <xsl:apply-imports/>
<table class="report data">
  <col class="itemNo"/>
  <thead>
    <tr>
      <td/>
      <th>Client Lit Code #</th>
      <th>Description</th>
      <xsl:if test="$xxshowRevision='Yes'">
      <th>Revision</th>
      </xsl:if>
      <th>ACC CDE</th>
      <th>Allow Web</th>
      <th class="number">Available Balance</th>
      <th class="number">Total Shipped</th>
      <th class="number">UOM</th>
    <xsl:call-template name="groupHead">
      <xsl:with-param name="i">1</xsl:with-param>
      <xsl:with-param name="count">
        <xsl:value-of select="$maxGroupCols"/>
      </xsl:with-param>
    </xsl:call-template>
    </tr>
  </thead>
  <tbody>
    <xsl:for-each select="Inventory[ count(. | key( 'byGroup', '../Item/Sku[.=current()/Sku]/../Groups/Group[1]')[1]) = 1 ]">
      <xsl:sort select="../Item/Sku[.=current()/Sku]/../Groups"/>
      <xsl:sort select="../Item/Sku[.=current()/Sku]/../CustomerPart"/>
      <xsl:sort select="Sku"/>
      <xsl:sort select="RevisionNo"/>

      <xsl:variable name="Item"
          select="../Item[./Sku=current()/Sku]"/>

      <xsl:element name="tr">
        <xsl:attribute name="class">
          <xsl:choose>
            <xsl:when test="position()!=last() and position() mod 5 = 0"
              >bgLight </xsl:when>
          </xsl:choose>
        </xsl:attribute>
        <xsl:apply-templates select="." mode="item"/>
      </xsl:element>

      <xsl:element name="tr">
        <xsl:attribute name="class">
          <xsl:choose>
            <xsl:when test="position()!=last() and position() mod 5 = 0"
              >bgLight </xsl:when>
          </xsl:choose>
        </xsl:attribute>
        <xsl:apply-templates select="." mode="group" />
      </xsl:element>
    </xsl:for-each>
  </tbody>
</table>
  </xsl:template>

  <!-- Define a rule for each inventory group -->
  <xsl:template match="Inventory" mode="group">
<td></td> <!-- ThumbImage -->
<td></td> <!-- PartNo -->
<td></td> <!-- Description -->
    <xsl:if test="$xxshowRevision='Yes'">
<td></td> <!-- RevisionNo -->
    </xsl:if>
<td></td> <!-- Access -->
<td></td> <!-- Web -->
<td></td> <!-- OnHand, LowestUOM -->
<td></td> <!-- OrderedBySku -->
<td></td> <!-- UomQty, UOM -->

  <xsl:call-template name="emptyGroupCols">
    <xsl:with-param name="i">1</xsl:with-param>
    <xsl:with-param name="count">
        <xsl:value-of select="$maxGroupCols"/>
    </xsl:with-param>
  </xsl:call-template>

  </xsl:template>

  <!-- Define a rule for each inventory item -->
  <xsl:template match="Inventory" mode="item">
    <xsl:variable name="Item"
        select="../Item[./Sku=current()/Sku]"/>

    <xsl:variable name="Revision"
        select="../Revision[./Sku=current()/Sku and
                            ./RevisionNo=current()/RevisionNo]"/>

    <xsl:variable name="CustPart"
        select="normalize-space($Item/CustomerPart)"/>

    <xsl:variable name="PartNo">
      <xsl:choose>
        <xsl:when test="string-length($CustPart)>0">
          <xsl:value-of select="$CustPart"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="Sku"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- Date format must be MM/DD/YYYY to work! -->
    <xsl:variable name="Year2Date"
        select="../Usage[starts-with(DateRange/Start, '01/01/')]
                        [starts-with(DateRange/End,   '12/31/')]"/>

    <xsl:variable name="Month2Date"
        select="../Usage[DateRange/Start != $Year2Date/DateRange/Start or
                         DateRange/End   != $Year2Date/DateRange/End    ]
                        [substring(DateRange/Start, 1, 3) =
                         substring(DateRange/End,   1, 3)]"/>

    <xsl:variable name="AllDates"
        select="../Usage[not(DateRange/Start)]
                        [starts-with(DateRange/End,   '12/31/')]"/>

    <xsl:variable name="SkuThumbImage"
         select=" $ThumbNames/ThumbList/ThumbFile[@Item=$Item/Sku]/File"/>

    <xsl:variable name="ThumbImage">
        <xsl:choose>
            <xsl:when test="string-length($SkuThumbImage)>0">
                <xsl:value-of select="$SkuThumbImage"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select=" $ThumbNames/ThumbList/ThumbFile[@Item=$Item/CustomerPart]/File"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="UOM">
        <xsl:choose>
            <xsl:when test="string-length(normalize-space($Item/UOM))>0">
                <xsl:value-of select="$Item/UOM"/>
            </xsl:when>
            <xsl:when test="string-length(normalize-space($Item/LowestUOM))>0">
                <xsl:value-of select="$Item/LowestUOM"/>
            </xsl:when>
            <xsl:otherwise>EA</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="LowestUOM">
        <xsl:choose>
            <xsl:when test="string-length(normalize-space($Item/LowestUOM))>0">
                <xsl:value-of select="$Item/LowestUOM"/>
            </xsl:when>
            <xsl:otherwise>EA</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

<td><xsl:if test="normalize-space($ThumbImage) != ''">
    <xsl:element name="img">
        <xsl:attribute name="src">
            <xsl:text>/wmsImages/SALIX/products/</xsl:text>
            <xsl:value-of select="$ThumbImage" />
        </xsl:attribute>
    </xsl:element>
    </xsl:if></td>
<td><xsl:value-of select="$PartNo"/></td>
<td><xsl:value-of select="$Item/Description"/></td>
    <xsl:if test="$xxshowRevision='Yes'">
<td><xsl:value-of select="RevisionNo"/></td>
    </xsl:if>
<td><xsl:value-of select="$Item/Access"/></td>
<td><xsl:value-of select="$Item/Web"/></td>
<td class='number'><xsl:value-of
    select="concat( format-number(OnHand,$rptqty,'f0'), $LowestUOM )"/></td>
<td class='number'><xsl:value-of
    select="format-number(sum($AllDates/OrderedBySku
            [Sku=current()/Sku]/ShippedQty),
            $rptqty,'f0')"/></td>
<td class='number'><xsl:value-of
    select="concat( $Item/UomQty, $UOM )"/></td>

  <xsl:variable name="countGroup"
    select="count($Item/Groups/Group )"/>

  <xsl:variable name="useNumGroupCols">
    <xsl:choose>
        <xsl:when test="$countGroup &gt;= $maxGroupCols">
            <xsl:value-of select="$maxGroupCols"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$countGroup"/>
        </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:for-each select="$Item/Groups/Group">
    <xsl:if test="position() &lt;= $useNumGroupCols">
        <td><xsl:value-of select="."/></td>
    </xsl:if>
  </xsl:for-each>

  <xsl:if test="$maxGroupCols &gt; $useNumGroupCols">
    <xsl:call-template name="emptyGroupCols">
      <xsl:with-param name="i">
        <xsl:value-of select="$useNumGroupCols"/>
        </xsl:with-param>
      <xsl:with-param name="count">
        <xsl:value-of select="$maxGroupCols"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:if>

  </xsl:template>

  <xsl:template name="emptyGroupCols">
    <xsl:param name="i" />
    <xsl:param name="count" />

    <xsl:if test="$i &lt;= $count">
      <td></td>
    </xsl:if>

    <!--begin_: RepeatTheLoopUntilFinished-->
    <xsl:if test="$i &lt;= $count">
        <xsl:call-template name="emptyGroupCols">
            <xsl:with-param name="i">
                <xsl:value-of select="$i + 1"/>
            </xsl:with-param>
            <xsl:with-param name="count">
                <xsl:value-of select="$count"/>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:if>

  </xsl:template>

  <xsl:template name="groupHead">
    <xsl:param name="i" />
    <xsl:param name="count" />

    <xsl:if test="$i &lt;= $count">
      <th>Group <xsl:value-of select="$i"/></th>
    </xsl:if>

    <!--begin_: RepeatTheLoopUntilFinished-->
    <xsl:if test="$i &lt;= $count">
        <xsl:call-template name="groupHead">
            <xsl:with-param name="i">
                <xsl:value-of select="$i + 1"/>
            </xsl:with-param>
            <xsl:with-param name="count">
                <xsl:value-of select="$count"/>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:if>

  </xsl:template>

</xsl:stylesheet>

感谢您提供的任何帮助。

编辑(2014 年 4 月 28 日): Here is a zip 包含所有必要的文件,或者try here if non-zip 更容易。请注意,“StaticFormatcsv.xsl”文件还有其他问题,我只包含它,因为它在我运行的命令中被引用。你现在可以忽略它。修复主 xsl 后,我将对“csv”xsl 应用相同的修复。

另外,这是我运行的命令:

java -cp /usr/share/java/xalan.jar org.apache.xalan.xslt.Process -in StaticData20140425154921 -out StaticReport20140425154921.html -PARAM csvLink StaticReport20140425154921.csv -PARAM thumbs ThumbList.xsl -xsl StaticFormat.xsl

其中“xalan.jar”指/指向“xalan-2.4.1.jar”。

【问题讨论】:

  • 我建议您 1. 尝试将问题简化为仅解决问题所必需的内容和 2. 发布代码使我们能够通过在不修改的情况下运行它来重现问题。目前,您的 XSLT 导入了一个我们看不到的文档,并且(因此?)引用了一个未注册的变量。
  • 我添加了运行它所需的确切代码。我目前无法简化它。网上有很多类似的例子,但都太简单了。因为我不知道是什么导致了问题,所以我不知道要删除什么以使其更简单。想法将不胜感激。
  • "我不知道要删除什么以使其更简单。" 您(和所有人)应该熟悉的两种基本故障排除技术:1. 将问题减半 - 即测试问题是在过程的前半部分还是后半部分产生。重复,直到您将问题的位置减少到可以在合理的时间和精力内进行详细检查的区域; 2. 尝试通过从头开始逐步构建代码来重现问题。

标签: xml xslt xalan


【解决方案1】:

我首先将您的 XSLT 简化为......

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Report">
    <html>
      <head>
        <title>Inventory with Shipping Data</title>
      </head>
      <body>
        <xsl:apply-templates select="Result"/>
      </body>
    </html>
  </xsl:template>

  <xsl:key name="byGroup" match="Inventory" use="../Item/Sku[.=current()/Sku]/../Groups/Group[1]" />

  <xsl:template match="Result">
    <table class="report data">
      <tbody>
        <xsl:for-each select="Inventory[ count(. | key( 'byGroup', '../Item/Sku[.=current()/Sku]/../Groups/Group[1]')[1]) = 1 ]">
          <xsl:element name="tr">
            <xsl:apply-templates select="." mode="item"/>
          </xsl:element>

          <xsl:element name="tr">
            <xsl:apply-templates select="." mode="group" />
          </xsl:element>
        </xsl:for-each>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="Inventory" mode="item">
      <td><xsl:value-of select="../Item/Sku[.=current()/Sku]/../Groups/Group[1]" /></td>
  </xsl:template>

  <xsl:template match="Inventory" mode="group">
      <td><xsl:value-of select="Sku" /></td>
  </xsl:template>
</xsl:stylesheet>

首先要注意的是,对于 XML 中的每个 Inventory 元素,您会输出两个 tr 元素,这就是为什么您会在输出中得到六行的原因。你总是会以这种方式获得偶数行。也许你想要你想要的是为每个不同的 Item/Groups/Group[1] (其中有两个不同的值)输出一行,然后为每个 Inventory 具有匹配的 Sku

首先,您的密钥可以简化为

<xsl:key name="byGroup" match="Inventory" 
         use="../Item[Sku=current()/Sku]/Groups/Group[1]" />

但是你如何使用这个键有问题......

<xsl:for-each 
     select="Inventory[ count(. | key( 'byGroup', '../Item/Sku[.=current()/Sku]/../Groups/Group[1]')[1]) = 1 ]">

key 函数的第二个参数不应使用撇号,否则将被视为文字。你应该这样做......(使用上面显示的简化键)

<xsl:for-each 
     select="Inventory[ count(. | key( 'byGroup', ../Item[Sku=current()/Sku]/Groups/Group[1])[1]) = 1 ]">

但即使这样也有问题。在 xsl:key 语句中,current() 将引用当前匹配的 Inventory 项。但是在 xsl:for-each 语句中,current() 指的是当 xsl:for-each 时你当前所在的节点> 被执行,也就是 Result 元素。

要解决此问题,请定义一个键以允许您按 Sku

查找 Item 元素
<xsl:key name="itemBySku" match="Item" use="Sku" />

那么,xsl:for-each 可以这样写:

<xsl:for-each 
     select="Inventory[ count(. | key('byGroup', key('itemBySku', Sku)/Groups/Group[1])[1]) = 1 ]">

最后,要输出当前组中的所有 Inventory 元素,您可以这样做:

<xsl:apply-templates 
     select="key('byGroup', key('itemBySku', Sku)/Groups/Group[1])" mode="group" />

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Report">
    <html>
      <head>
        <title>Inventory with Shipping Data</title>
      </head>
      <body>
        <xsl:apply-templates select="Result"/>
      </body>
    </html>
  </xsl:template>

  <xsl:key name="byGroup" match="Inventory" use="../Item[Sku=current()/Sku]/Groups/Group[1]" />
  <xsl:key name="itemBySku" match="Item" use="Sku" />

  <xsl:template match="Result">
    <table class="report data">
      <tbody>
        <xsl:for-each select="Inventory[ count(. | key('byGroup', key('itemBySku', Sku)/Groups/Group[1])[1]) = 1 ]">
          <tr>
            <xsl:apply-templates select="." mode="item"/>
          </tr>

          <xsl:apply-templates select="key('byGroup', key('itemBySku', Sku)/Groups/Group[1])" mode="group" />
        </xsl:for-each>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="Inventory" mode="item">
    <td><xsl:value-of select="key('itemBySku', Sku)/Groups/Group[1]" /></td>
  </xsl:template>

  <xsl:template match="Inventory" mode="group">
    <tr>
      <td><xsl:value-of select="Sku" /></td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

这可能不是您真正想要的,但它至少应该向您展示如何简化您的 XSLT,并为您提供一些可用作添加您需要但不相关的所有其他功能的基础到手头的根本问题。

【讨论】:

  • 您的想法向我展示了一些替代方案。我不得不做一些小的调整,但一旦我这样做了,它就完全符合我的要求。非常感谢。我不得不承认,当你提到简化键定义,然后指出我用错了键时,我不得不拍了拍我的额头——完全有道理。此外,您关于如何简化 XSLT 的示例也有帮助。再次感谢。
猜你喜欢
  • 2014-08-03
  • 2022-01-15
  • 2018-06-29
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
相关资源
最近更新 更多