简体   繁体   English

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

[英]Build XML structure from attributes in single node

I have the following XML source: 我有以下XML源:

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

In any order, I need to loop through specific attributes (ie only ones which are related to the HTML I'm building: bold/superscript/subscript, etc.) and where one of these attributes evaluates to 'true', output nested elements to get the following output: 在任何顺序中,我需要循环遍历特定属性(即只有与我正在构建的HTML相关的属性:bold / superscript / subscript等),并且其中一个属性评估为'true',输出嵌套元素获得以下输出:

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

I have a feeling I need to use some sort of recursion like follows (without the infinite loop, of course): 我有一种感觉,我需要使用某种递归,如下所示(当然没有无限循环):

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

Any ideas? 有任何想法吗? I'm looking for the cleanest XSLT 2.0 solution and appreciate the help. 我正在寻找最干净的XSLT 2.0解决方案并感谢您的帮助。

Thanks, 谢谢,

This is a nice use case for <xsl:next-match/> : 这是<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>

When you apply templates to an element element it'll first fire the highest priority matching template, and if that template uses next-match it will delegate to the next highest priority, etc. Your example element in the question matches the first, second and fourth templates above, so initially the "bold" template would be selected, then that would delegate to the "superscript" one and finally to the generic element one, resulting in <strong><sup>stuff</sup></strong> . 当您将模板应用于element元素时,它将首先触发最高优先级匹配模板,如果该模板使用next-match ,它将委托给下一个最高优先级,等等。问题中的示例element与第一个,第二个和上面的第四个模板,所以最初将选择“粗体”模板,然后将其委托给“上标”一个,最后委托给通用element一个,从而产生<strong><sup>stuff</sup></strong>

As you can see from this example, the priority numbers are what determines the order of nesting - if the priorities of the second and fourth templates were reversed you'd get <sup><strong>stuff</strong></sup> instead. 正如您在此示例中所看到的,优先级数字决定了嵌套的顺序 - 如果第二个和第四个模板的优先级被颠倒,您将获得<sup><strong>stuff</strong></sup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM