XSLT 混合内容节点
我有一个很愚蠢的问题。如何确保我的 XML 混合内容节点不会混淆?比如说,我有一个与此类似的 XML 结构。
<root>
<book>
<title>Stuff</title>
<description> This book is <i>great</i> if you need to know about stuff.
I suggest <link ref="Things">this one</link> if you need to know
about things. </description>
</book>
[other books]
</root>
我需要最终的内容看起来像这样
<h1>List of books</h1>
<h2><a name="Stuff"/>Stuff</h2>
<p> This book is <i>great</i> if you need to know about stuff.
I suggest <a href="#Things">this one</a> if you need to know
about things. </p>
,但我无法提取文本节点的部分,我总是抓住整个内容。我正在使用下降轴。知道我做错了什么吗?
这是我的 xslt:
<xsl:template match="description/*">
<xsl:for-each select="following-sibling::*">
<xsl:choose>
<xsl:when test="name(.)='link'">
<a href="{@ref}"><xsl:value-of select="."/></a>
</xsl:when>
<xsl:when test="name(.)='em'">
<em><xsl:value-of select="."/></em>
</xsl:when>
<xsl:otherwise><p><xsl:value-of select="."/></p></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
请注意,所附的 XML 和生成的 html 只是示例,为了清楚起见,我必须处理一个更大的结构,我没有将其包含在其中。
I have a quite stupid question. How can I make sure that my XML mixed content node doesn't get mixed up? I have, say, an XML structure resembling this.
<root>
<book>
<title>Stuff</title>
<description> This book is <i>great</i> if you need to know about stuff.
I suggest <link ref="Things">this one</link> if you need to know
about things. </description>
</book>
[other books]
</root>
I need the final content to look like this
<h1>List of books</h1>
<h2><a name="Stuff"/>Stuff</h2>
<p> This book is <i>great</i> if you need to know about stuff.
I suggest <a href="#Things">this one</a> if you need to know
about things. </p>
But I can't extract the portions of the text node, I always grab the whole thing. I'm using the descendant axis. Any clue what I'm doing wrong?
Here is my xslt:
<xsl:template match="description/*">
<xsl:for-each select="following-sibling::*">
<xsl:choose>
<xsl:when test="name(.)='link'">
<a href="{@ref}"><xsl:value-of select="."/></a>
</xsl:when>
<xsl:when test="name(.)='em'">
<em><xsl:value-of select="."/></em>
</xsl:when>
<xsl:otherwise><p><xsl:value-of select="."/></p></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
Please note that the enclosed XML and resulting html are merely examples, I have to deal with a bigger structure which I'm not enclosing in, for sake of clarity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是您的朋友:为您的输入 XML 生成以下输出(注意:为了可读性,我通过 tidy 对其进行了管道处理。不相关的白色-在此过程中删除了空间):
<xsl:apply-templates>
is your friend:The following output is generated for your input XML (Note: I piped it through tidy for the sake of readability. Non-relevant white-space was removed in the process):