XSLT:XML 节点呈现问题
XSLT: XML node rendering issue
我正在尝试使用 XSLT 呈现示例 XML,如下所示:
<?xml version="1.0" encoding="UTF-16"?>
<root>
<title>test</title>
<description> This is the first description</description>
<description>This is for
<subject>testing</subject>every day
</description>
</root>
我正在使用以下 XSLT 代码来显示描述节点。
<xsl:for-each select="root/description">
<p><xsl:value-of select="."/></p>
</xsl:for-each>`
这是我得到的输出。
This is the first description
This is for testing every day
请问为什么在第二个描述节点显示testing?
testing在主题节点下。由于格式化,我想使用 <xsl:value-of select="subject"/>
代码获取主题节点。
能否请您提出解决方案?
非常感谢。
问候,AK
在 XSLT-1.0 中,表达式 <xsl:value-of select="."/>
select 是所有 descendant 节点的 text()
值并将它们连接起来。对于 select 仅所有直接子级,您必须像这样为每个子级应用另一个:
<xsl:for-each select="root/description">
<p>
<xsl:for-each select="text()"> <!- Select all direct text() children -->
<xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
</xsl:for-each>
</p>
</xsl:for-each>
然后,输出将如下所示:
<p>This is the first description </p>
<p>This is for every day </p>
编辑(有额外要求):
您可以将 text()
节点与特定的父元素匹配:
<xsl:template match="/">
<xsl:for-each select="root/description">
<p><xsl:apply-templates select="node()|@*" /></p>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="subject/text()">
<b><xsl:value-of select="normalize-space(.)"/></b>
</xsl:template>
输出为:
<p>This is the first description </p>
<p>
This is for <b>testing</b>
every day </p>
这种方法可以在输出中添加高亮元素。但是我不知道如何去掉多余的空格,所以这(也许)已经很好了。
xsl:value-of
只能创建字符串。如果要在输出中包含 subject
元素,请使用 xsl:copy-of
而不是 xsl:value-of
。
我正在尝试使用 XSLT 呈现示例 XML,如下所示:
<?xml version="1.0" encoding="UTF-16"?>
<root>
<title>test</title>
<description> This is the first description</description>
<description>This is for
<subject>testing</subject>every day
</description>
</root>
我正在使用以下 XSLT 代码来显示描述节点。
<xsl:for-each select="root/description">
<p><xsl:value-of select="."/></p>
</xsl:for-each>`
这是我得到的输出。
This is the first description
This is for testing every day
请问为什么在第二个描述节点显示testing?
testing在主题节点下。由于格式化,我想使用 <xsl:value-of select="subject"/>
代码获取主题节点。
能否请您提出解决方案?
非常感谢。
问候,AK
在 XSLT-1.0 中,表达式 <xsl:value-of select="."/>
select 是所有 descendant 节点的 text()
值并将它们连接起来。对于 select 仅所有直接子级,您必须像这样为每个子级应用另一个:
<xsl:for-each select="root/description">
<p>
<xsl:for-each select="text()"> <!- Select all direct text() children -->
<xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
</xsl:for-each>
</p>
</xsl:for-each>
然后,输出将如下所示:
<p>This is the first description </p>
<p>This is for every day </p>
编辑(有额外要求):
您可以将 text()
节点与特定的父元素匹配:
<xsl:template match="/">
<xsl:for-each select="root/description">
<p><xsl:apply-templates select="node()|@*" /></p>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="subject/text()">
<b><xsl:value-of select="normalize-space(.)"/></b>
</xsl:template>
输出为:
<p>This is the first description </p>
<p>
This is for <b>testing</b>
every day </p>
这种方法可以在输出中添加高亮元素。但是我不知道如何去掉多余的空格,所以这(也许)已经很好了。
xsl:value-of
只能创建字符串。如果要在输出中包含 subject
元素,请使用 xsl:copy-of
而不是 xsl:value-of
。