实际上,如我所见,我可以添加名称空间。因为我非常接近输出,所以我希望看到。第一个代码:
XML:
<helptext>
<h6>General configuration options.</h6>
<h2>Changing not yet supported.</h2>
<p>this is a <b>paragraph</b><br/>this is a new line</p>
</helptext>XSL:
<xsl:template name="transformHelptext">
<xsl:for-each select="./child::*">
<xsl:element name="ht:{local-name()}">
<xsl:choose>
<xsl:when test="count(./child::*)>0">
<xsl:call-template name="transformHelptext"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:for-each>
</xsl:template>到目前一切尚好。<h6>..</h6>和<h2>...</h2>线路没有问题。但是第三行有一个子节点,它是一个<b>。不知何故,“段落”是此行显示的唯一文本。我在choose语句中有一个错误。但我想不出来。
谢谢
P.S : ht命名空间在xsl-stylesheet标记中定义,它是'xmlns:ht="http://www.w3.org/1999/xhtml"‘
附言:我尝试做的是,使在我特定的xml节点上应用html标记、样式成为可能。
发布于 2011-04-19 17:41:43
也许可以像这样:
<xsl:template name="transformHelptext">
<xsl:apply-templates select="@*|node()" />
</xsl:template>
<xsl:template match="*" >
<xsl:element name="ht:{local-name(.)}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@*|text()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>在"transformHelptext“模板中,选择所有属性和节点,并对其应用模板。
第二个模板匹配元素节点并更改名称空间。第三个模板匹配属性和文本节点,只创建一个副本。
发布于 2011-04-19 17:56:13
输入XML :
<?xml version="1.0" encoding="UTF-8"?>
<helptext>
<h6>General configuration options.</h6>
<h2>Changing not yet supported.</h2>
<p>this is a <b>paragraph</b><br/>this is a new line</p>
</helptext>XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*|@*">
<xsl:element name="ht:{local-name()}" namespace="http://www.w3.org/1999/xhtml">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
输出XML :
<?xml version="1.0" encoding="UTF-8"?>
<ht:helptext xmlns:ht="http://www.w3.org/1999/xhtml">
<ht:h6>General configuration options.</ht:h6>
<ht:h2>Changing not yet supported.</ht:h2>
<ht:p>
this is a
<ht:b>paragraph</ht:b>
<ht:br />
this is a new line
</ht:p>
</ht:helptext>讨论:尽可能多地使用,避免使用<xsl:for-each>,因为它会降低处理器的速度。
https://stackoverflow.com/questions/5713400
复制相似问题