xml - 使用 xsl 时避免代码重复 :choose and xsl:when

标签 xml xslt xpath xslt-1.0

有没有一种巧妙的方法来简化以下样式表以避免重复整个 when当每个变量之间只有一个变量发生变化时阻塞?

理想情况下,我想要这样的东西,在 $i 上循环 6 次:

<xsl:when test="$depth &gt; $i">
    [...]
    <xsl:value-of select="substring($npath,($nlength - $i*2),1) - 1"/>
    [...]
</xsl:when>

我正在使用 XSLT 1.0。

XML 输入
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl" version="1.0"?>
<root>
  <item>Main_A
    <item>Item_A</item>
    <item>Item_B
      <item>Subitem_A</item>
      <item>Subitem_B</item>
    </item>
    <item>Item_C</item>
  </item>
  <item>Main_B
    <item>Item_A
      <item>Subitem_A</item>
      </item>
    <item>Item_B</item>
  </item>
</root>

XSLT 1.0 样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">
  <html>
    <body>
      <xsl:apply-templates select="item">
        <xsl:with-param name="depth" select="1"/>
      </xsl:apply-templates>
    </body>
  </html>
</xsl:template>

<xsl:template match="item">
  <xsl:param name="depth" select="1"/>
  <xsl:if test="$depth &lt; 10">
    <ul>
      <li>
        <xsl:text>path</xsl:text>
        <xsl:call-template name="loopnumformat">
          <xsl:with-param name="depth" select="$depth"/>
        </xsl:call-template>
        <xsl:text> = </xsl:text>
        <xsl:apply-templates match="item">
          <xsl:with-param name="depth" select="$depth + 1"/>
        </xsl:apply-templates>
      </li>
    </ul>
  </xsl:if>
</xsl:template>

<xsl:template name="loopnumformat">
  <xsl:param name="depth" select="1"/>
  <xsl:variable name="npath">
    <xsl:number level="multiple" from="*[10]"/>
  </xsl:variable>
  <xsl:variable name="nlength">
    <xsl:value-of select="string-length($npath)"/>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$depth &gt; 2">
      <xsl:text>:</xsl:text>
      <xsl:value-of select="substring($npath,($nlength - 2*2),1) - 1"/>
      <xsl:call-template name="loopnumformat">
        <xsl:with-param name="depth" select="$depth - 1"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$depth &gt; 1">
      <xsl:text>:</xsl:text>
      <xsl:value-of select="substring($npath,($nlength - 1*2),1) - 1"/>
      <xsl:call-template name="loopnumformat">
        <xsl:with-param name="depth" select="$depth - 1"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$depth &gt; 0">
      <xsl:text>:</xsl:text>
      <xsl:value-of select="substring($npath,($nlength - 0*2),1) - 1"/>
      <xsl:call-template name="loopnumformat">
        <xsl:with-param name="depth" select="$depth - 1"/>
      </xsl:call-template>
    </xsl:when>
  </xsl:choose>

</xsl:template>

</xsl:stylesheet>

HTML 输出
<html>
<body>
  <ul>
    <li>path:0 = Main_A
      <ul>
        <li>path:0:0 = Item_A</li>
      </ul>
      <ul>
        <li>path:0:1 = Item_B
          <ul>
            <li>path:0:1:0 = Subitem_A</li>
          </ul>
          <ul>
            <li>path:0:1:1 = Subitem_B</li>
          </ul>
        </li>
      </ul>
      <ul>
        <li>path:0:2 = Item_C</li>
      </ul>
    </li>
  </ul>
  <ul>
    <li>path:1 = Main_B
      <ul>
        <li>path:1:0 = Item_A
          <ul>
            <li>path:1:0:0 = Subitem_A</li>
          </ul>
        </li>
      </ul>
      <ul>
        <li>path:1:1 = Item_B</li>
      </ul>
    </li>
  </ul>
</body>
</html>

最佳答案

Is there a clever way to simplify the following stylesheet in order to avoid repeating a whole when block when only one variable is changing between each of those?



这取决于具体情况,但在您的特定情况下,您可以这样做的一种方法是移动 xsl:choose内部,因此在每种情况下相同的部分仅表示一次:
  <xsl:text>:</xsl:text>
  <xsl:choose>
    <xsl:when test="$depth &gt; 2">
      <xsl:value-of select="substring($npath,($nlength - 2*2),1) - 1"/>
    </xsl:when>
    <xsl:when test="$depth &gt; 1">
      <xsl:value-of select="substring($npath,($nlength - 1*2),1) - 1"/>
    </xsl:when>
    <xsl:when test="$depth &gt; 0">
      <xsl:value-of select="substring($npath,($nlength - 0*2),1) - 1"/>
    </xsl:when>
  </xsl:choose>
  <xsl:call-template name="loopnumformat">
    <xsl:with-param name="depth" select="$depth - 1"/>
  </xsl:call-template>

但是您似乎会竭尽全力使用xsl:number当它不能完全满足您的需求时。看起来你可以更简单地完成整个事情:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="root">
    <html>
      <body>
        <xsl:apply-templates select="item" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <xsl:param name="path-prefix" select="'path'"/>
    <!-- if there are fewer than nine ':' characters in the path prefix for
         this item ... -->
    <xsl:if test="string-length($path-prefix) - string-length(translate($path-prefix, ':', '')) &lt; 9">
      <xsl:variable name="my-path"
          select="concat($path-prefix, ':', position() - 1)" />
      <ul>
        <li>
          <xsl:value-of select="$my-path"/>
          <xsl:text> = </xsl:text>
          <xsl:apply-templates select="text()[1]"/>
          <xsl:apply-templates select="item">
            <xsl:with-param name="path-prefix" select="$my-path"/>
          </xsl:apply-templates>
        </li>
      </ul>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

也许你甚至可以摆脱 xsl:if ,似乎它的存在可能是为了满足 loopnumformat 的限制。原始样式表中的模板——这个版本没有内在的深度限制。请注意,它确实假定您要复制到输出文档中的文本节点将被限制为每个 <item> 中的第一个节点。 ,但是修改样式表以复制所有样式表很容易。

关于xml - 使用 xsl 时避免代码重复 :choose and xsl:when,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804782/

相关文章:

C#/LINQ : How to Query this XML Structure

xml - XSLT 无法正常工作以删除元素

python - 如何使用 selenium 和 python 创建具有相同 xpath 的元素列表?

dom - 与importXML一起使用的XPath表达式

xml - 你可以放一个xsl :if test inside a Choose statement in an xslt?

xml - 给定一个引用表,如何为每个引用反向查找数字对象标识符?

java - 扩展自定义布局类 - 从未使用过构造函数

ios - 从 AS3 保存到 XML(最终用于 iOS)

c# - XSLT:使用闭合标签传输 xml

xml - 改变元素的命名空间