多商品分类树的XSL结构 [英] XSL structure for category tree for multiple goods

查看:16
本文介绍了多商品分类树的XSL结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将XML结构更改为具有多个这样的良好实体时,here描述了如何使工作成为解决方案:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Work>    
<Good id = "1">
    <list  num="1050" id = "2531" desc="List 1">
        <part  num="1">
            <pos  isKey="0" id="2532" pid="2531" desc="Part 1" />
            <pos  num="1.2." isKey="0" id="2554" pid="2532" desc="Position 1.2" />
            <pos  num="1.2.6." isKey="1" id="2591" pid="2554" desc="Position 1.2.6" />
          </part>
        </list>
        <list  num="1090" id = "3029" desc="List 2">
          <part  num="2">
            <pos  isKey="0" id="3033" pid="3029" desc="Category 2" />
            <pos  isKey="0" id="3040" pid="3033" desc="Part 9" />
            <pos  num="9.2." isKey="0" id="3333" pid="3040" desc="Position 9.2" />
            <pos  num="9.2.1." isKey="0" id="3334" pid="3333" desc="Position 9.2.1" />
            <pos  num="9.2.1.2" isKey="1" id="3339" pid="3334" desc="Position 9.2.1.2" />
        </part>
    </list>
</Good>
<Good id = "2">
    <list  num="1050" id = "2531" desc="List 3">
        <part  num="1">
            <pos  isKey="0" id="2532" pid="2531" desc="Part 1" />
            <pos  num="1.2." isKey="0" id="2554" pid="2532" desc="Position 1.2" />
            <pos  num="1.2.6." isKey="0" id="2591" pid="2554" desc="Position 1.2.6" />
            <pos  num="1.2.6.1." isKey="1" id="2592" pid="2591" desc="Position 1.2.6.1" />       
         </part>
     </list>      
 </Good>
</Work>

我尝试为Work/Good Entity创建For-Each循环,但无济于事:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />

  <!-- key to look up any element with an id attribute based on the value of
       that id -->
  <xsl:key name="elementsByPid" match="*[@pid]" use="@pid" />

  <xsl:template match="/">
    <html>
      <body>
      <h2>Lists</h2>
        <xsl:apply-templates select="Work/Goods" />
      </body>
    </html>
  </xsl:template>

<xsl:template match="Work/Goods">
    <xsl:for-each select="Work/Goods">
        <xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:apply-templates select="Work/Goods/list"  />
</xsl:template>

<xsl:template match="Work/Goods/list">
    <xsl:for-each select="Work/Goods/list">
        <xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:apply-templates select="."  mode="table"/>
</xsl:template>

<xsl:template match="*" mode="table">
    <xsl:variable name="shouldOutput">
      <xsl:apply-templates select="." mode="shouldOutput" />
    </xsl:variable>
    <xsl:if test="string-length($shouldOutput)">
      <table>
        <xsl:apply-templates select="." />
      </table>
    </xsl:if>
  </xsl:template>

  <!-- the main recursive logic - first produce output for this row, then
       process any of the children (in the id->pid chain) that need to be
       output -->
  <xsl:template match="*">
    <xsl:apply-templates select="." mode="row" />
    <xsl:for-each select="key('elementsByPid', @id)">
      <xsl:variable name="shouldOutput">
        <xsl:apply-templates select="." mode="shouldOutput" />
      </xsl:variable>
      <xsl:if test="string-length($shouldOutput)">
        <xsl:apply-templates select="." />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="*" mode="row">
    <tr>
      <td colspan="2"><xsl:value-of select="@desc" /></td>
    </tr>
  </xsl:template>

  <!-- special case for pos elements with a @num - produce two columns -->
  <xsl:template match="pos[@num]" mode="row">
    <tr>
      <td><xsl:value-of select="@num" /></td>
      <td><xsl:value-of select="@desc" /></td>
    </tr>
  </xsl:template>

  <!-- check whether this node should be output by checking whether it, or any
       of its descendants in the id->pid tree, has @out=1.  The template will
       return an empty RTF for nodes that should not be output, and an RTF
       containing a text node with one or more "1" characters for nodes that
       should.  -->
  <xsl:template match="*[@out='1']" mode="shouldOutput">1</xsl:template>
  <xsl:template match="*" mode="shouldOutput">
    <xsl:apply-templates select="key('elementsByPid', @id)"
         mode="shouldOutput"/>
  </xsl:template>

</xsl:stylesheet>
enter code here

模板中存在不允许此代码工作的内容。
我还应该更改哪些内容才能使其正常工作?

推荐答案

为此不需要使用任何for-each,只需删除两个模板

<xsl:template match="Work/Goods">

<xsl:template match="Work/Goods/list">

完全,并将根模板更改为简单

  <xsl:template match="/">
    <html>
      <body>
      <h2>Lists</h2>
        <xsl:apply-templates select="Work/Good/list" mode="table" />
      </body>
    </html>
  </xsl:template>

不需要显式迭代,因为单个select表达式将取出Good元素中的所有list元素(请注意,您的XML示例中的元素名为Good,而不是Goods))。

这篇关于多商品分类树的XSL结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆