Xml 如何在XSLT中为混合项目符号列表创建模板
如何制作一个模板,使以下XMLXml 如何在XSLT中为混合项目符号列表创建模板,xml,templates,xslt,Xml,Templates,Xslt,如何制作一个模板,使以下XML <content> <bullet>text</bullet> <bullet>more text</bullet> o more text o more text o more text <bullet>more text</bullet> </content> 文本 更多文本 o更多文本 o更多文本 o更多文本 更多文本 在html中看起来像这样 <li
<content>
<bullet>text</bullet>
<bullet>more text</bullet>
o more text
o more text
o more text
<bullet>more text</bullet>
</content>
文本
更多文本
o更多文本
o更多文本
o更多文本
更多文本
在html中看起来像这样
<li>text</li
<li>more text</li>
o more text
o more text
o more text
<li>more text</li>
text基本上,您需要一个匹配所有内容的模板,然后根据输入内容显示输出。我使用node()
作为模板匹配,然后使用
标记确定我使用的是纯文本还是
节点的内容,并相应地显示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="node()">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="node()">
<li><xsl:value-of select="."/></li>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
您可以使用此工具测试样式表:(不幸的是,此应用程序没有永久链接功能)。我得到以下输出:
<li>text</li>
<li>more text</li>
o more text
o more text
o more text
<li>more text</li>
文本
更多文本
o更多文本
o更多文本
o更多文本
更多文本
省略select=“@*| node()”
属性,它们介于不必要和错误之间。第一个模板可以而且应该完全忽略,因为文本节点的默认规则将完成任务。输出应指定为HTML。删除条带空格,这是不必要的。@torazaburo您的陈述和您对这个问题的回答都是“不必要的”……您想要的输出不是一致的HTML,因为元素只能包含- 元素,而不能包含原始文本。难道你真的不想把这三行
o多个文本转换成子项目符号吗?这是伪装成XSLT的基本方法。@torazaburo:你说得对,我是XSLT noob:)。谢谢你的回答,我从中学到了很多。
<li>text</li>
<li>more text</li>
o more text
o more text
o more text
<li>more text</li>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- processes all *nodes* by copying them,
and can be overridden for individual
elements, attributes, comments, processing instructions,
or text nodes -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- replace content with ul -->
<xsl:template match="content">
<ul>
<xsl:apply-templates select="@*|node()"/>
</ul>
</xsl:template>
<!-- replace bullet with li -->
<xsl:template match="bullet">
<li>
<xsl:apply-templates select="@*|node()"/>
</li>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="content">
<ul><xsl:apply-templates/></ul>
</xsl:template>
<xsl:template match="bullet">
<li><xsl:apply-templates/></li>
</xsl:template>
</xsl:stylesheet>