如何解决在子节点问题中添加名称空间前缀
| 实际上,正如我所见,我可以添加名称空间。因为我非常希望看到输出。第一个代码: 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>
。而“ paragraph”是此行中唯一显示的文本。我在ѭ5陈述中有误。但我无法弄清楚。
谢谢
P.S:ht名称空间是在xsl-stylesheet标记中定义的,它是\'xmlns:ht = \“ http://www.w3.org/1999/xhtml \” \'
附言:我想做的是,可以在我的特定xml节点上应用html标签,样式解决方法
也许是这样的:
<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”模板中,选择所有属性和节点并将模板应用于它们。
第二个模板匹配元素节点并更改名称空间。第三个模板匹配属性和文本节点,仅创建一个副本。,输入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>
,因为它会减慢处理器的速度。版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。