embedding tags in xslt
I have this xslt which is working:
<xsl:when test=开发者_运维百科"area_of_expertise">
<div>
<xsl:value-of select="area_of_expertise"/>
</div>
</xsl:when>
but what i need is along the lines of:
<xsl:when test="area_of_expertise">
<div id="<xsl:value-of select="area_of_expertise"/>">
<xsl:value-of select="area_of_expertise"/>
</div>
</xsl:when>
However the second example has errors.. does anyone know why?
Btw Is there a way we can transform the node's name area_of_expertise
into areaOfExperiseLabel
and insert that as the id? the output that i really need is this:
<div id="areaOfExpertiseLabel">
asasdasdasd
</div>
The reason it errors out is because it's no longer valid XML.
To do what you're trying to do:
<xsl:when test="title">
<div id="{title}">
<xsl:value-of select="title"/>
</div>
</xsl:when>
You can put any sort of selector inside of the {} tags, or even reference variables if you have something complex.
<xsl:variable name="some_complex_variable">
<xsl:value-of select="title"/>
</xsl:variable>
<xsl:when test="title">
<div id="{$some_complex_variable}">
<xsl:value-of select="title"/>
</div>
</xsl:when>
A 3rd, long-winded way of doing it is to dynamically attach the attribute with xsl:attribute:
<xsl:when test="title">
<div>
<xsl:attribute name="id" select="title"/>
</div>
</xsl:when>
For 2nd part try using this template:
<xsl:template name="parse">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:if test="$position <= string-length($input)">
<xsl:choose>
<xsl:when test="substring($input, $position, 1) = '_'">
<xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:call-template name="parse">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + 2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($input, $position, 1)"/>
<xsl:call-template name="parse">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
Usage:
<xsl:call-template name="parse">
<xsl:with-param name="input" select="'area_of_expertise'"/>
<xsl:with-param name="position" select="1"/>
</xsl:call-template>
For the second part, converting from underscores to camel case, you may want to look at String Processing in the XSLT Standard Library. With str:subst()
to split at underscores, str:to-camelcase()
to change letter case suitably, and concat()
to add the "Label" suffix, you should be set.
For the 1st part you could use:
<xsl:when test="area_of_expertise">
<div>
<xsl:attribute name="id">
<xsl:value-of select="area_of_expertise"/>
</xsl:attribute>
<xsl:value-of select="area_of_expertise"/>
</div>
</xsl:when>
It might be a silly answer, but it seems that you need this simple trace:
<xsl:when test="area_of_expertise">
<div id="areaOfExperiseLabel">
<xsl:value-of select="area_of_expertise"/>
</div>
</xsl:when>
Otherwise, why are you intersted in <xsl:value-of select="area_of_expertise"/>
for @id
if you then need another string?
精彩评论