首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向子节点添加命名空间前缀问题

向子节点添加命名空间前缀问题
EN

Stack Overflow用户
提问于 2011-04-19 16:08:27
回答 2查看 1.4K关注 0票数 0

实际上,如我所见,我可以添加名称空间。因为我非常接近输出,所以我希望看到。第一个代码:

XML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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标记、样式成为可能。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-19 17:41:43

也许可以像这样:

代码语言:javascript
复制
<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“模板中,选择所有属性和节点,并对其应用模板。

第二个模板匹配元素节点并更改名称空间。第三个模板匹配属性和文本节点,只创建一个副本。

票数 1
EN

Stack Overflow用户

发布于 2011-04-19 17:56:13

输入XML :

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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 :

代码语言:javascript
复制
<?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>,因为它会降低处理器的速度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5713400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档