从单个节点中的属性构建XML结构
发布时间:2020-12-16 23:14:19 所属栏目:百科 来源:网络整理
导读:我有以下 XML源: element not-relevant="true" bold="true" superscript="true" subscript="false" text="stuff"/ 在任何顺序中,我需要循环遍历特定属性(即只有与我正在构建的HTML相关的属性:bold / superscript / subscript等),并且其中一个属性评估为’t
我有以下
XML源:
<element not-relevant="true" bold="true" superscript="true" subscript="false" text="stuff"/> 在任何顺序中,我需要循环遍历特定属性(即只有与我正在构建的HTML相关的属性:bold / superscript / subscript等),并且其中一个属性评估为’true’,输出嵌套元素获得以下输出: <strong> <sup> stuff </sup> </strong> 我有一种感觉,我需要使用某种递归,如下所示(当然没有无限循环): <xsl:template match="element"> <xsl:call-template name="content"> <xsl:with-param name="text" select="@text"/> </xsl:call-template> </xsl:template> <xsl:template name="content"> <xsl:param name="text"/> <xsl:choose> <xsl:when test="@bold = 'true'"> <strong> <xsl:copy> <xsl:call-template name="content"> <xsl:with-param name="text" select="$text"/> </xsl:call-template> <xsl:copy> </strong> </xsl:when> <xsl:when test="@subscript = 'true'"> <sub> <xsl:copy> <xsl:call-template name="content"> <xsl:with-param name="text" select="$text"/> </xsl:call-template> <xsl:copy> </sub> </xsl:when> <xsl:when test="@superscript = 'true'"> <sup> <xsl:copy> <xsl:call-template name="content"> <xsl:with-param name="text" select="$text"/> </xsl:call-template> <xsl:copy> </sup> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text" disable-output-escaping="yes"/> </xsl:otherwise> </xsl:choose> </xsl:template> 有任何想法吗?我正在寻找最干净的XSLT 2.0解决方案并感谢您的帮助. 谢谢, 解决方法
这是
<xsl:next-match/> 的一个很好的用例:
<xsl:template match="element" priority="1"> <xsl:value-of select="@text" /> </xsl:template> <xsl:template match="element[@superscript = 'true']" priority="2"> <sup><xsl:next-match/></sup> </xsl:template> <xsl:template match="element[@subscript = 'true']" priority="3"> <sub><xsl:next-match/></sub> </xsl:template> <xsl:template match="element[@bold = 'true']" priority="4"> <strong><xsl:next-match/></strong> </xsl:template> 当您将模板应用于元素元素时,它将首先触发最高优先级匹配模板,如果该模板使用下一个匹配,它将委托给下一个最高优先级,等等.问题中的示例元素与第一个,第二个和上面的第四个模板,所以最初选择“粗体”模板,然后将其委托给“上标”一个,最后委托给通用元素一个,从而产生< strong>< sup> stuff< / sup>< /强取代. 正如您在此示例中所看到的,优先级数字决定了嵌套的顺序 – 如果第二个和第四个模板的优先级被颠倒,您将得到< sup>< strong> stuff< / strong>< / SUP>代替. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |