开发者

Removing white space inside quotes in XSLT

I'm trying to format a table from XML. Lets say I have this line in the XML

<country>Dominican Republic</country>

I would like to get my table to look like this

<td class="country DominicanRepublic">Dominican Republic</td>

I've tried this:

<td class="country {country}"><xsl:value-of select="country"/></td>

then this:

<xsl:element name="td">
 <xsl:attribute name="class">
  <xsl:text>country </xsl:text>
  <xsl:value-of select="normalize-space(country)"/>
 </xsl:attribute>
<xsl:value-of select="country"/>
</xsl:element>

The normalize-space() doesn't remove the space between the two parts of the name and I can't use开发者_运维技巧 <xsl:strip-space elements="country"/> because I need the space when I display the name inside the table cell.

How can I strip the space from the value inside the class, but not the text in the cell?


Use the translate() function to replace spaces ' ' with nothing '':

<xsl:element name="td">
    <xsl:attribute name="class">
        <xsl:text>country </xsl:text>
        <xsl:value-of select="translate(country,' ','')"/>
        </xsl:attribute>
    <xsl:value-of select="country"/>
</xsl:element>

You can use normalize-space(), which will remove any leading and trailing white space and convert multiple spaces between characters into a single space. Then, send the results through translate() to replace any remaining spaces:

<xsl:element name="td">
   <xsl:attribute name="class">
    <xsl:text>country </xsl:text>
    <xsl:value-of select="translate(normalize-space(country),' ','')"/>
    </xsl:attribute>
    <xsl:value-of select="normalize-space(country)"/>
</xsl:element>


You will need to split your string by whitespaces recursively, have a look at this topic: Does XSLT have a Split() function?

Or you can try this replace function implementation: http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜