templates - xsl :fo template match not firing on nested list

标签 templates xslt xsl-fo

我需要在应用 xsl:fo 的嵌套列表上使用连字符作为项目符号。我有两个要匹配的模板,但只应用了一个。如果我只使用第一个模板,则外部列表将应用该模板。如果我仅使用第二个模板,则嵌套列表将应用该模板。我尝试在第二个模板中匹配的模式是任何以列表项作为父级的无序列表。非常感谢任何有关获得我想要的输出的帮助。

XML

<ThisNode>
<ul>
    <li>Item One</li>
    <li>Item Two</li>
    <li>Item Three
        <ul>
            <li>Sub-Item One</li>
            <li>Sub-Item Two</li>
        </ul>
    </li>
    <li>Item Four</li>
    <li>Item Five</li>
</ul>
</ThisNode>

XSLT

<xsl:template match="ul">
<fo:list-block>
    <xsl:for-each select="./li">
        <fo:list-item>
            <fo:list-item-label end-indent="label-end()">
                <fo:block font-weight="bold">•</fo:block>
            </fo:list-item-label>
            <fo:list-item-body start-indent="8pt">
                <fo:block><xsl:value-of select="."/></fo:block>
            </fo:list-item-body>
        </fo:list-item>
    </xsl:for-each>
</fo:list-block>
</xsl:template>

<xsl:template match="li//ul">
<fo:list-block start-indent="8pt">
    <xsl:for-each select="./li">
        <fo:list-item>
            <fo:list-item-label end-indent="label-end()">
                <fo:block font-weight="bold">-</fo:block>
            </fo:list-item-label>
            <fo:list-item-body start-indent="16pt">
                <fo:block><xsl:value-of select="."/></fo:block>
            </fo:list-item-body>
        </fo:list-item>
    </xsl:for-each>
</fo:list-block>
</xsl:template>

<fo:block text-align="left">
    <xsl:apply-templates select="./ThisNode"/>
</fo:block>

期望输出

• Item One
• Item Two
• Item Three
    - Sub-Item One
    - Sub-Item Two
• Item Four
• Item Five

仅使用第一个模板或同时使用两个模板的实际输出

• Item One
• Item Two
• Item ThreeSub-Item OneSub-Item Two
• Item Four
• Item Five

仅使用第二个模板的实际输出

Item One Item Two Item Three
    - Sub-Item One
    - Sub-Item Two
Item Four Item Five

最佳答案

这看起来是一个带有模式调用模板的好地方,可以区分这两种情况。外层模板调用内层模板:

<?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:template match="/">
      <fo:block text-align="left">
         <xsl:apply-templates select="ThisNode"/>
      </fo:block>
   </xsl:template>


   <xsl:template match="ul">
      <fo:list-block>
         <xsl:for-each select="li">
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block font-weight="bold">•</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="8pt">
                  <fo:block>
                     <!-- I'm not quite sure what you want here -->
                     <xsl:value-of select="text()"/>
                     <xsl:apply-templates select="ul" mode="inner"/>
                  </fo:block>
               </fo:list-item-body>
            </fo:list-item>
         </xsl:for-each>
      </fo:list-block>
   </xsl:template>

   <xsl:template match="ul" mode="inner">
      <fo:list-block start-indent="8pt">
         <xsl:for-each select="./li">
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block font-weight="bold">-</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="16pt">
                  <fo:block>
                     <xsl:value-of select="."/>
                  </fo:block>
               </fo:list-item-body>
            </fo:list-item>
         </xsl:for-each>
      </fo:list-block>
   </xsl:template>

</xsl:stylesheet>

关于templates - xsl :fo template match not firing on nested list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21702040/

相关文章:

c++ - C++17和C++20在模板友元函数中一元和二元运算符的区别

c++ - 是否可以使用模板元编程有条件地禁用全局函数定义?

xml - 如何使用 match 属性获取具有特定名称的所有节点

xslt - 使用 XSL 进行绝对定位转换并呈现​​为 PDF

css - XSL-FO 中是否内置了 "like"CSS?

xml - 我在哪里可以找到关于 XSL-FO(格式化/ed 对象)的好教程,这些教程是用于 fop 和获取 PDF 的?

c++ - 使用 C++ 模板实现访问者模式

c++ - 我怎样才能有一个模板元素的容器

xml - 使用XSLT基于字符串模式自动创建xml模板

xml - XSLT document() 在 WebKit 浏览器中的使用