分组方法来维护嵌套元素XSLT 2.0 - 分离多余的元素

时间:2011-04-18 19:43:37

标签: xslt sequence xslt-grouping

我正在对项目符号项进行分组,并将它们嵌入到某些列表元素中的第二个项目符号项列表中。我可以将项目分组,但我的递归模板正在从我在doc中的当前位置到最后选择所有内容。我需要为嵌套列表设置一个结束点。

我的输出如下:

<ul>
    <li>First Bullet:<ul>
         <li>Sub-bullet 1: info for sub-bullet
                                            1</li>
         <li>Sub-bullet 2: info for sub-bullet
                                            2</li>
         <li>Sub-bullet 3: info for sub-bullet
                                            3</li>
         <li>Sub-bullet 4: info for sub-bullet
                                                4</li>
         <li>Sub-bullet 5: info for sub-bullet
                                            5</li>
         <li>Sub-bullet 6: info for sub-bullet
                                            6</li>
        </ul>
    </li>

    <li>Second Bullet:<ul>
         <li>Sub-bullet 5: info for sub-bullet
                                            5</li>
         <li>Sub-bullet 6: info for sub-bullet
                                            6</li>
        </ul>
    </li>
</ul>

但它应该看起来像这样(在第一个li下没有Sub-bullets 5和6):

<ul>
    <li>First Bullet:<ul>
         <li>Sub-bullet 1: info for sub-bullet
                                            1</li>
         <li>Sub-bullet 2: info for sub-bullet
                                            2</li>
         <li>Sub-bullet 3: info for sub-bullet
                                            3</li>
         <li>Sub-bullet 4: info for sub-bullet
                                                4</li>
        </ul>
    </li>

    <li>Second Bullet:<ul>
         <li>Sub-bullet 5: info for sub-bullet
                                            5</li>
         <li>Sub-bullet 6: info for sub-bullet
                                            6</li>
        </ul>
    </li>
</ul>

我不确定如何在XSLT中正确分组子项目以使它们处于正确的位置。

这是我的来源:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<office:document-content xmlns:dom="http://www.w3.org/2001/xml-events"
  xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
  xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
  xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
  xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
  xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
  xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
  xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
  xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
  xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0">
  <office:body>
    <office:presentation>
      <draw:page draw:name="Slide1" draw:style-name="a1271"
        draw:master-page-name="Master1-Layout3-cust-Bullet"
        presentation:presentation-page-layout-name="Master1-PPL3">
        <draw:frame draw:id="id109" presentation:style-name="a1039"
                    draw:name="Content Placeholder 1" svg:x="0.31258in" svg:y="1.29095in"
                    svg:width="9.33243in" svg:height="3.8175in" presentation:class="outline"
                    presentation:placeholder="false">
                    <draw:text-box>
                        <text:list text:style-name="a1012">
                            <text:list-item>
                                <text:p text:style-name="a1011" text:class-names=""
                                    text:cond-style-name=""><text:span text:style-name="a1010"
                                        text:class-names="">First Bullet:</text:span></text:p>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1015">
                            <text:list-item>
                                <text:list text:style-name="a1015">
                                    <text:list-item>
                                        <text:p text:style-name="a1014" text:class-names=""
                                            text:cond-style-name=""><text:span
                                                text:style-name="a1013" text:class-names=""
                                                >Sub-bullet 1: info for sub-bullet
                                            1</text:span></text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1018">
                            <text:list-item>
                                <text:list text:style-name="a1018">
                                    <text:list-item>
                                        <text:p text:style-name="a1017" text:class-names=""
                                            text:cond-style-name=""><text:span
                                                text:style-name="a1016" text:class-names=""
                                                >Sub-bullet 2: info for sub-bullet
                                            2</text:span></text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1021">
                            <text:list-item>
                                <text:list text:style-name="a1021">
                                    <text:list-item>
                                        <text:p text:style-name="a1020" text:class-names=""
                                            text:cond-style-name=""><text:span
                                                text:style-name="a1019" text:class-names=""
                                                >Sub-bullet 3: info for sub-bullet
                                            3</text:span></text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1025">
                            <text:list-item>
                                <text:list text:style-name="a1025">
                                    <text:list-item>
                                        <text:p text:style-name="a1024" text:class-names=""
                                            text:cond-style-name=""><text:span
                                                text:style-name="a1022" text:class-names=""
                                                >Sub-bullet 4: info for sub-bullet
                                                4</text:span><text:span text:style-name="a1023"
                                                text:class-names=""/></text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1029">
                            <text:list-item>
                                <text:p text:style-name="a1028" text:class-names=""
                                    text:cond-style-name=""><text:span text:style-name="a1026"
                                        text:class-names="">Second Bullet:</text:span><text:span
                                        text:style-name="a1027" text:class-names=""/></text:p>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1032">
                            <text:list-item>
                                <text:list text:style-name="a1032">
                                    <text:list-item>
                                        <text:p text:style-name="a1031" text:class-names=""
                                            text:cond-style-name=""><text:span
                                                text:style-name="a1030" text:class-names=""
                                                >Sub-bullet 5: info for sub-bullet
                                            5</text:span></text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1035">
                            <text:list-item>
                                <text:list text:style-name="a1035">
                                    <text:list-item>
                                        <text:p text:style-name="a1034" text:class-names=""
                                            text:cond-style-name=""><text:span
                                                text:style-name="a1033" text:class-names=""
                                                >Sub-bullet 6: info for sub-bullet
                                            6</text:span></text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                    </draw:text-box>
                    <svg:desc/>
                </draw:frame>
      </draw:page>
    </office:presentation>
  </office:body>
</office:document-content>

这是我的样式表:

<xsl:stylesheet
    version="2.0"
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
    xmlns:math="http://www.w3.org/1998/Math/MathML"
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
    xmlns:dom="http://www.w3.org/2001/xml-events"
    xmlns:xforms="http://www.w3.org/2002/xforms"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
    office:class="text"
    office:version="1.0"
    exclude-result-prefixes="#all">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="draw:frame">
<ul>
 <xsl:for-each-group select="draw:text-box/text:list" group-starting-with="text:list-item">
  <xsl:for-each select="current-group()">

        <xsl:apply-templates select="text:list-item[not(text:list)]"/>

    </xsl:for-each>
</xsl:for-each-group> 
</ul>
</xsl:template>

<xsl:template match="draw:text-box/text:list/text:list-item/text:list/text:list-item/text:p">
<li><xsl:value-of select="."/></li>
</xsl:template>


<xsl:template match="draw:text-box/text:list/text:list-item/text:p">
<li><xsl:value-of select="."/>
    <!-- Check for nested list -->
    <xsl:if test="../../following-sibling::text:list[text:list-item/text:list]">

            <xsl:for-each select="current-group()">
            <xsl:apply-templates select="text:list-item/text:p"/>
            </xsl:for-each>

            </xsl:for-each-group>
    </ul>
    </xsl:if>
</li>
</xsl:template>


</xsl:stylesheet>

有关如何在2.0中完成此操作的任何建议?我试图创建一些计数变量,但是在当前组上下文之外建立端点还没有运气。我知道必须有一种明显(更优雅)的方式来完成这项工作。

2 个答案:

答案 0 :(得分:0)

您需要一个与文本列表匹配的模板;这将输出&lt; ul&gt;标签

答案 1 :(得分:0)

有一个简短易用的解决方案,根本不需要XSLT 2.0:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
 exclude-result-prefixes="text"
 >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text:list">
  <ul><xsl:apply-templates/></ul>
 </xsl:template>

 <xsl:template match="text:list-item[not(text:list)]">
  <li><xsl:apply-templates/></li>
 </xsl:template>

 <xsl:template match=
  "text()[not(ancestor::text:list-item)]"/>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<office:document-content xmlns:dom="http://www.w3.org/2001/xml-events"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"   xmlns:xlink="http://www.w3.org/1999/xlink"   xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0">
    <office:body>
        <office:presentation>
            <draw:page draw:name="Slide1" draw:style-name="a1271"         draw:master-page-name="Master1-Layout3-cust-Bullet"         presentation:presentation-page-layout-name="Master1-PPL3">
                <draw:frame draw:id="id109" presentation:style-name="a1039"                     draw:name="Content Placeholder 1" svg:x="0.31258in" svg:y="1.29095in"                     svg:width="9.33243in" svg:height="3.8175in" presentation:class="outline"                     presentation:placeholder="false">
                    <draw:text-box>
                        <text:list text:style-name="a1012">
                            <text:list-item>
                                <text:p text:style-name="a1011" text:class-names=""                                     text:cond-style-name="">
                                    <text:span text:style-name="a1010"                                         text:class-names="">First Bullet:</text:span>
                                </text:p>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1015">
                            <text:list-item>
                                <text:list text:style-name="a1015">
                                    <text:list-item>
                                        <text:p text:style-name="a1014" text:class-names=""                                             text:cond-style-name="">
                                            <text:span                                                 text:style-name="a1013" text:class-names=""                                                 >Sub-bullet 1: info for sub-bullet                                             1</text:span>
                                        </text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1018">
                            <text:list-item>
                                <text:list text:style-name="a1018">
                                    <text:list-item>
                                        <text:p text:style-name="a1017" text:class-names=""                                             text:cond-style-name="">
                                            <text:span                                                 text:style-name="a1016" text:class-names=""                                                 >Sub-bullet 2: info for sub-bullet                                             2</text:span>
                                        </text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1021">
                            <text:list-item>
                                <text:list text:style-name="a1021">
                                    <text:list-item>
                                        <text:p text:style-name="a1020" text:class-names=""                                             text:cond-style-name="">
                                            <text:span                                                 text:style-name="a1019" text:class-names=""                                                 >Sub-bullet 3: info for sub-bullet                                             3</text:span>
                                        </text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1025">
                            <text:list-item>
                                <text:list text:style-name="a1025">
                                    <text:list-item>
                                        <text:p text:style-name="a1024" text:class-names=""                                             text:cond-style-name="">
                                            <text:span                                                 text:style-name="a1022" text:class-names=""                                                 >Sub-bullet 4: info for sub-bullet                                                 4</text:span>
                                            <text:span text:style-name="a1023"                                                 text:class-names=""/>
                                        </text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1029">
                            <text:list-item>
                                <text:p text:style-name="a1028" text:class-names=""                                     text:cond-style-name="">
                                    <text:span text:style-name="a1026"                                         text:class-names="">Second Bullet:</text:span>
                                    <text:span                                         text:style-name="a1027" text:class-names=""/>
                                </text:p>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1032">
                            <text:list-item>
                                <text:list text:style-name="a1032">
                                    <text:list-item>
                                        <text:p text:style-name="a1031" text:class-names=""                                             text:cond-style-name="">
                                            <text:span                                                 text:style-name="a1030" text:class-names=""                                                 >Sub-bullet 5: info for sub-bullet                                             5</text:span>
                                        </text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                        <text:list text:style-name="a1035">
                            <text:list-item>
                                <text:list text:style-name="a1035">
                                    <text:list-item>
                                        <text:p text:style-name="a1034" text:class-names=""                                             text:cond-style-name="">
                                            <text:span                                                 text:style-name="a1033" text:class-names=""                                                 >Sub-bullet 6: info for sub-bullet                                             6</text:span>
                                        </text:p>
                                    </text:list-item>
                                </text:list>
                            </text:list-item>
                        </text:list>
                    </draw:text-box>
                    <svg:desc/>
                </draw:frame>
            </draw:page>
        </office:presentation>
    </office:body>
</office:document-content>

产生了想要的正确结果

<ul>
   <li>First Bullet:</li>
</ul>
<ul>
   <ul>
      <li>Sub-bullet 1: info for sub-bullet                                             1</li>
   </ul>
</ul>
<ul>
   <ul>
      <li>Sub-bullet 2: info for sub-bullet                                             2</li>
   </ul>
</ul>
<ul>
   <ul>
      <li>Sub-bullet 3: info for sub-bullet                                             3</li>
   </ul>
</ul>
<ul>
   <ul>
      <li>Sub-bullet 4: info for sub-bullet                                                 4</li>
   </ul>
</ul>
<ul>
   <li>Second Bullet:</li>
</ul>
<ul>
   <ul>
      <li>Sub-bullet 5: info for sub-bullet                                             5</li>
   </ul>
</ul>
<ul>
   <ul>
      <li>Sub-bullet 6: info for sub-bullet                                             6</li>
   </ul>
</ul>

,浏览器将其显示为

       
  • First Bullet:
       
            
    • 子弹1:子弹1的信息
    •    
       
            
    • 子弹2:子弹2的信息
    •    
       
            
    • 子弹3:子弹3的信息
    •    
       
            
    • 子弹4:子弹4的信息
    •    
       
  • 第二个子弹:
       
            
    • 子弹5:子弹5的信息
    •    
       
            
    • 子弹6:子弹6的信息
    •