XSLT variable into the curly braces

2019-03-14 19:51发布

What is the meaning of curled brackets {} in the following sample (in preceding lines, the variable $fieldName is initialized and populated with string):

<xsl:element name="{$fieldName}">
    <xsl:apply-templates select="field"/>
</xsl:element>

标签: xslt
3条回答
聊天终结者
2楼-- · 2019-03-14 20:18

This is a bit hard to find, but it's discussed in Creating Elements with xsl:element.

The xsl:element element allows an element to be created with a computed name [from an expression]. The expanded-name of the element to be created is specified by a required name attribute ..

While the {} syntax is not explicitly discussed here, the meaning of the braces is used similarly in other contexts, such as Creating Elements and Attributes and applies here as well.

The value of an attribute of a literal result element is interpreted as an attribute value template: it can contain [XPath] expressions contained in curly braces ({}).

In this case, $fieldName is just an XPath expression for a variable which should evaluate to a valid element name.

查看更多
爷的心禁止访问
3楼-- · 2019-03-14 20:36

You can use these curly braces (attribute value templates) whenever you need to compute an expression in attributes which would otherwise treat the contents as text.

For example, suppose you have a XML source this one:

<link site="www.stackoverflow.com"/>

and you would like to generate an HTML link from it like

<a href="http://www.stackoverflow.com">Click here</a>

If you simply read the contents of @site into the href attribute like this:

<xsl:template match="link">
    <a href="http://@site">Click here</a>
</xsl:template>

it won't work, since it will be treated as plain text and you will get:

<a href="http://@site">Click here</a>

But if you wrap the @site in curly braces:

<xsl:template match="link">
    <a href="http://{@site}">Click here</a>
</xsl:template>

It will be treated as XPath, will be executed and you will get:

<a href="http://www.stackoverflow.com">Click here</a>

If it weren't for the curly braces, you would need to use <xsl:attribute> in <a> containing an <xsl:value-of> to obtain the same result:

<xsl:template match="link">
    <a>
        <xsl:attribute name="href">
            <xsl:text>http://</xsl:text><xsl:value-of select="@site"/>
        </xsl:attribute>
        <xsl:text>Link</xsl:text>
    </a>
</xsl:template>

In your example, the name atrribute of <xsl:element> requires a string. To treat that string as an XPath expression and replace it with the result of the variable $fieldName, you either place it within curly braces as you did, or you use the <xsl:attribute> element as above:

<xsl:element>
    <xsl:attribute name="name">
        <xsl:value-of select="$fieldName"/>
    </xsl:attribute>
    <xsl:apply-templates select="field"/>
</xsl:element/>
查看更多
Fickle 薄情
4楼-- · 2019-03-14 20:37

These are called Attribute Value Templates. See Here for details w3.org

Definition: In an attribute that is designated as an attribute value template, such as an attribute of a literal result element, an expression can be used by surrounding the expression with curly brackets ({}).

An attribute value template consists of an alternating sequence of fixed parts and variable parts. A variable part consists of an XPath expression enclosed in curly brackets ({}). A fixed part may contain any characters, except that a left curly bracket must be written as {{ and a right curly bracket must be written as }}.

查看更多
登录 后发表回答