维护嵌套元素的分组方法 XSLT 2.0 - 分离多余的
我将项目符号项目进行分组,并将它们作为项目符号项目的第二个列表嵌入到某些列表元素中。我可以将项目分组,但我的递归模板正在选择从文档中当前位置到末尾的所有内容。我需要为嵌套列表设置一个终点。
我的输出看起来像这样:
<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 下没有子项目符号 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 中实现此目的有什么建议吗?我尝试创建一些计数变量,但在当前组上下文之外建立端点方面还没有任何运气。我知道必须有一种明显的(而且更优雅的)方法来完成这项工作。
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-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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个简短、简单且容易的解决方案,根本不需要 XSLT 2.0:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
浏览器显示为:
There is a short, simple and easy solution that doesn't require XSLT 2.0 at all:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
and the browser displays this as:
您需要一个与文本列表匹配的模板;这将输出
;标签。
You need a template that matches a text-list; this would output the <ul> tag.