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

问题描述:

其实我看,我可以添加名字空间。因为我非常接近我期望看到的输出。首先代码:添加命名空间前缀子节点问题

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声明中有错误。但我无法弄清楚。

由于

PS:HT命名空间在XSL样式表的标记定义和它是 '的xmlns:HT = “http://www.w3.org/1999/xhtml”'

PS:我尝试做的是,使之可以应用HTML标签类似这样的替代,在我的具体XML节点风格

也许一些:

<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” templeate,选择所有ATTR同步和节点并将模板应用于它们。

第二模板相匹配元素节点,并且改变的命名空间。第三个模板匹配属性和文本节点,并创建一个副本。

+0

谢谢您的回复。但它确实显示纯文本,除非浏览器是Firefox。 – savruk 2011-04-19 09:54:49

+0

@savruk也许XML头缺少 – Stephan 2011-04-19 10:14:12

+0

其实我没有得到它与您的代码工作,谢谢 – savruk 2011-04-19 10:15:05

输入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>,因为它会降低处理器速度。

+0

+1。 – savruk 2011-04-19 10:51:27