Xml 使用xsl:choose和xsl:when时避免代码重复

Xml 使用xsl:choose和xsl:when时避免代码重复,xml,xslt,xpath,xslt-1.0,Xml,Xslt,Xpath,Xslt 1.0,是否有一种聪明的方法来简化以下样式表,以避免在每个样式表之间只有一个变量发生变化时重复整个块 理想情况下,我希望这样,在$I上循环6次: <xsl:when test="$depth &gt; $i"> [...] <xsl:value-of select="substring($npath,($nlength - $i*2),1) - 1"/> [...] </xsl:when> [...] [...] 我正在使用XSL

是否有一种聪明的方法来简化以下样式表,以避免在每个样式表之间只有一个变量发生变化时重复整个

理想情况下,我希望这样,在
$I
上循环6次:

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

[...]
[...]
我正在使用XSLT1.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>
<?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>
<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>

主楼A
项目A
项目B
分项A
分项B
项目C
主楼
项目A
分项A
项目B
XSLT1.0样式表

<?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>
<?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>
<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>

  • 路径 =
: : :
HTML输出

<?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>
<?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>
<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>

  • 路径:0=主路径A
    • 路径:0:0=项目A
    • 路径:0:1=项目
      • 路径:0:1:0=子项_A
      • 路径:0:1:1=子项
    • 路径:0:2=项目
  • 路径:1=主路径
    • 路径:1:0=项目A
      • 路径:1:0:0=子项_A
    • 路径:1:1=项目
有没有一种聪明的方法来简化下面的样式表,以避免在每个样式表之间只更改一个变量时重复整个when块

这取决于具体情况,但在您的特定情况下,一种方法是将
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:if
,它似乎是为了满足原始样式表中
loopnumformat
模板的限制而出现的——这个版本没有固有的深度限制。请注意,它确实假定要复制到输出文档中的文本节点将被限制在每个
中的第一个,但是修改样式表以复制所有这些节点是很容易的

有没有一种聪明的方法来简化下面的样式表,以避免在每个样式表之间只更改一个变量时重复整个when块

这取决于具体情况,但在您的特定情况下,一种方法是将
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:if
,它似乎是为了满足原始样式表中
loopnumformat
模板的限制而出现的——这个版本没有固有的深度限制。请注意,它确实假定要复制到输出文档中的文本节点将限制在每个
中的第一个,但修改样式表以复制所有节点将非常容易。

看起来您在这里做得太过火了。您只是在寻找一种方法来计算前面的
节点,递归地向上遍历文档树

这个简单的转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="item">
      <li>
        <xsl:text>path</xsl:text>
        <xsl:apply-templates select="." mode="path" />
        <xsl:text> = </xsl:text>
        <xsl:value-of select="normalize-space(text()[1])" />
        <xsl:if test="item">
          <ul>
            <xsl:apply-templates select="item" />
          </ul>
        </xsl:if>
      </li>
  </xsl:template>

  <xsl:template match="item" mode="path">
      <xsl:apply-templates select="parent::item" mode="path" />
      <xsl:text>:</xsl:text>
      <xsl:value-of select="count(preceding-sibling::item)" />
  </xsl:template>

</xsl:stylesheet>

  • 路径 =
  • :
    结果:

    <html>
       <body>
          <ul>
             <li>path:0 = Main_A
                <ul>
                   <li>path:0:0 = Item_A</li>
                   <li>path:0:1 = Item_B
                      <ul>
                         <li>path:0:1:0 = Subitem_A</li>
                         <li>path:0:1:1 = Subitem_B</li>
                      </ul>
                   </li>
                   <li>path:0:2 = Item_C</li>
                </ul>
             </li>
             <li>path:1 = Main_B
                <ul>
                   <li>path:1:0 = Item_A
                      <ul>
                         <li>path:1:0:0 = Subitem_A</li>
                      </ul>
                   </li>
                   <li>path:1:1 = Item_B</li>
                </ul>
             </li>
          </ul>
       </body>
    </html>
    
    
    
    • 路径:0=主路径A
      • 路径:0:0=项目A
      • 路径:0:1=项目
        • 路径:0:1:0=子项_A
        • 路径:0:1:1=子项
      • 路径:0:2=项目
    • 路径:1=主路径
      • 路径:1:0=项目A
        • 路径:1:0:0=子项_A
      • 路径:1:1=项目

    看来你在这里做工程太过分了。您只是在寻找一种方法来计算前面的
    节点,递归地向上遍历文档树

    这个简单的转换:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="root">
        <html>
          <body>
            <ul>
              <xsl:apply-templates select="item" />
            </ul>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="item">
          <li>
            <xsl:text>path</xsl:text>
            <xsl:apply-templates select="." mode="path" />
            <xsl:text> = </xsl:text>
            <xsl:value-of select="normalize-space(text()[1])" />
            <xsl:if test="item">
              <ul>
                <xsl:apply-templates select="item" />
              </ul>
            </xsl:if>
          </li>
      </xsl:template>
    
      <xsl:template match="item" mode="path">
          <xsl:apply-templates select="parent::item" mode="path" />
          <xsl:text>:</xsl:text>
          <xsl:value-of select="count(preceding-sibling::item)" />
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    
  • 路径 =
  • :
    结果:

    <html>
       <body>
          <ul>
             <li>path:0 = Main_A
                <ul>
                   <li>path:0:0 = Item_A</li>
                   <li>path:0:1 = Item_B
                      <ul>
                         <li>path:0:1:0 = Subitem_A</li>
                         <li>path:0:1:1 = Subitem_B</li>
                      </ul>
                   </li>
                   <li>path:0:2 = Item_C</li>
                </ul>
             </li>
             <li>path:1 = Main_B
                <ul>
                   <li>path:1:0 = Item_A
                      <ul>
                         <li>path:1:0:0 = Subitem_A</li>
                      </ul>
                   </li>
                   <li>path:1:1 = Item_B</li>
                </ul>
             </li>
          </ul>
       </body>
    </html>
    
    
    
    • 路径:0=主路径A
      • 路径:0:0=项目A
      • 路径:0:1=项目
        • 路径:0:1:0=子项_A
        • 路径:0:1:1=子项
      • 路径:0:2=项目
    • 路径:1=主路径
      • 路径:1:0=项目A
        • 路径:1:0:0=子项_A
      • 路径:1:1=项目

    在我看来,这里有一个更简单的解决实际问题的方法:在我看来,这里有一个更简单的解决实际问题的方法:非常感谢您的详细解释!这是一个我从未想过的非常优雅的解决方案。好像我