开发者

Grouping method to maintain nested elements XSLT 2.0 - separating the excess

I'm grouping bulleted items and embedding them as a second list of bulleted items inside some list elements. I can get the items to group, but my recursion template is selecting everything from my current position in the doc to the end. I need to set an ending point for my nested lists.

My output looks like this:

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

But it SHOULD look like this (without Sub-bullets 5 and 6 under the first li):

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

I'm not sure how to properly group the sub-bullets in XSLT to get them in the correct position.

Here's my source:

<?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-P开发者_运维问答PL3">
        <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>

Here's my stylesheet:

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

Any suggestions on how to accomplish this in 2.0? I've attempted to create some count variables, but haven't had any luck yet on establishing an endpoint outside of the current-group context. I know there must be an obvious (and more elegant) way to get this done.


There is a short, simple and easy solution that doesn't require XSLT 2.0 at all:

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

When this transformation is applied on the provided XML document:

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

the wanted, correct result is produced:

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

and the browser displays this as:

  • First Bullet:
    • Sub-bullet 1: info for sub-bullet 1
    • Sub-bullet 2: info for sub-bullet 2
    • Sub-bullet 3: info for sub-bullet 3
    • Sub-bullet 4: info for sub-bullet 4
  • Second Bullet:
    • Sub-bullet 5: info for sub-bullet 5
    • Sub-bullet 6: info for sub-bullet 6


You need a template that matches a text-list; this would output the <ul> tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜