最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xslt - Can't figure out how to use XSL variables - Stack Overflow

programmeradmin3浏览0评论

I'm using xmlstarlet and have spent hours reading without success. I have this in my XML:

<person pcode="sherry-smith">Sherry Smith</person>
<day>14</day>

I want the XSL to produce this line of HTML:

<span id="sherry-smith-14" onClick="getFile('sherry-smith','sherry-smith-14')">Sherry Smith</span>

I've tried many ways of doing this, all ending up with errors. Here's an example of what I've tried:

  <xsl:for-each select="person">
         <xsl:variable name="pc"> <xsl:value-of select="@pcode">-<xsl:value-of select="day">
           <span id="{$pc}"><xsl:value-of select="."/></span>
       </xsl:variable>
    </xsl:for-each>

Help, please.

I'm using xmlstarlet and have spent hours reading without success. I have this in my XML:

<person pcode="sherry-smith">Sherry Smith</person>
<day>14</day>

I want the XSL to produce this line of HTML:

<span id="sherry-smith-14" onClick="getFile('sherry-smith','sherry-smith-14')">Sherry Smith</span>

I've tried many ways of doing this, all ending up with errors. Here's an example of what I've tried:

  <xsl:for-each select="person">
         <xsl:variable name="pc"> <xsl:value-of select="@pcode">-<xsl:value-of select="day">
           <span id="{$pc}"><xsl:value-of select="."/></span>
       </xsl:variable>
    </xsl:for-each>

Help, please.

Share Improve this question edited Mar 17 at 14:55 Ed S. asked Mar 17 at 14:12 Ed S.Ed S. 1119 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Assuming that the context is the parent element where the person and day elements live, you could do something like this:

<xsl:variable name="pc" select="concat(person/@pcode, '-', day)"/>
<span id="{$pc}" onclick="getFile('{person/@pcode}', '{$pc}')">
  <xsl:value-of select="person"/>
</span>

Where you declare the variable $pc with the value of person/@pcode, -, and day and then (outside of the variable) construct the span element and use the $pc variable in the two places.

Note the use of the curly braces (attribute value templates) in order to reference the variables inside of the literal attribute declaration.

发布评论

评论列表(0)

  1. 暂无评论