Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从单个节点中的属性构建XML结构_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

从单个节点中的属性构建XML结构

从单个节点中的属性构建XML结构,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我有以下XML源代码: <element not-relevant="true" bold="true" superscript="true" subscript="false" text="stuff"/> 以任何顺序,我都需要循环特定属性(即,仅与我正在构建的HTML相关的属性:粗体/上标/下标等),其中一个属性的计算结果为“true”,输出嵌套元素以获得以下输出: <strong> <sup> stuff </

我有以下XML源代码:

<element not-relevant="true" bold="true" superscript="true" subscript="false" text="stuff"/>

以任何顺序,我都需要循环特定属性(即,仅与我正在构建的HTML相关的属性:粗体/上标/下标等),其中一个属性的计算结果为“true”,输出嵌套元素以获得以下输出:

<strong>
    <sup>
        stuff
    </sup>
</strong>

东西

我觉得我需要使用如下某种递归(当然没有无限循环):




有什么想法吗?我正在寻找最干净的XSLT2.0解决方案,非常感谢您的帮助


谢谢,

这是一个很好的用例:



当您将模板应用于
元素
元素时,它将首先触发最高优先级的匹配模板,如果该模板使用
下一个匹配
,它将委托给下一个最高优先级,以此类推。问题中的示例
元素
与上面的第一、第二和第四个模板匹配,因此最初为“粗体”将选择模板,然后将其委托给“上标”元素,最后委托给通用的
元素
one,从而产生
内容

从这个例子中可以看出,优先级数字决定了嵌套的顺序——如果第二个和第四个模板的优先级颠倒,您将得到
东西

<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>
<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>