2016-09-07 95 views
0

我们试图用占位符将HTML模板转换为使用XSLT的最终HTML。嵌套HTML span元素的转换

(简化)HTML模板的样子:

<p> 
    <span class="condition" id="v6">Some text here 
    <span class="placeholder" id="v1" /> 
    </span> 
</p> 

改造应

  • placeholder类取代每个span元素;
  • 隐藏或显示包含condition

每个span元素(简化)XSLT我们已经是:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
       xmlns:local="urn:local"     
       xmlns:s0="http://www.w3.org/1999/xhtml"> 
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" /> 

    <!-- Take the HTML template --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <!--Replace every placeholder in the HTML template with the value from the XML data--> 
    <xsl:template match="span[@class='placeholder']"> 
     <xsl:variable name="this" select="current()/@id"/> 
     <xsl:value-of select="'replaced'" /> 
    </xsl:template> 


    <!-- Handle conditions based on custom logic --> 
    <xsl:template match="span[@class='condition']"> 
    <xsl:variable name="this" select="current()/@id"/> 
    <xsl:if test="$this = 'v6'"> 
     <xsl:value-of select="current()" /> 
    </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

如果占位符跨度不嵌套到条件跨度,一切正常精细。然而,在上面的HTML例子,它不能正常工作,输出:

<p>Some text here</p> 

我们希望它是:

<p> 
    Some text here 
    replaced  
</p> 

看来,如果被执行的条件,但占位符未被执行或以某种方式被覆盖;所以基本上,占位符跨度元素不会被替换。

有没有人对此有过解释,我们做错了什么?

谢谢!

回答

0

同时找到了解决办法:

<!-- Handle conditions based on custom logic --> 
    <xsl:template match="span[@class='condition']"> 
    <xsl:variable name="this" select="current()/@id"/> 
    <xsl:if test="$this = 'v6'"> 
     <xsl:apply-templates select="node()"/> <<<<----------------  
    </xsl:if> 
    </xsl:template> 

而不是采取目前的案文,我只是重新申请的节点。