开发者

Remove white space in xsl

How can I get rid of the space that gets added after the "Location" element which is breaking the links? IE: "/location toxxx.aspx" XML

- <Root>
 <Schema>
 <Field Type="Text" DisplayName="Location" Required="FALSE" MaxLength="255"  Name="Location" /> 
 <Field Type="Currency" DisplayName="Price one way-saloon" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_one_x0020_way" /> 
 <Field Type="Currency" DisplayName="Price return-saloon" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_return" /> 
 <Field ReadOnly="TRUE" Type="Computed" Name="LinkTitle" DisplayName="Location id" /> 
 <Field Type="Currency" DisplayName="Price one way-estate" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_one_x0020_way_x002d_" /> 
 <Field Type="Currency" DisplayName="Price return-estate" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_return_x002d_estate" /> 
 <Field Type="Currency" DisplayName="Price one way-MPV" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_one_x0020_way_x002d_0" /> 
 <Field Type="Currency" DisplayName="Price return-MPV" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_return_x002d_MPV" /> 
 <Field Type="Currency" DisplayName="Price one way-MPV+" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_one_x0020_way_x002d_1" /> 
 <Field Type="Currency" DisplayName="Price return-MPV+" Required="FALSE" Decimals="0" LCID="2057" Name="Price_x0020_return_x002d_MPV_x00" /> 
 </Schema>
 <Data ItemCount="1">
 <Row Location="" Price_x0020_one_x0020_way="" Price_x0020_return="" LinkTitle="" Price_x0020_one_x0020_way_x002d_="" Price_x0020_return_x002d_estate="" Price_x0020_one_x0020_way_x002d_0="" Price_x0020_return_x002d_MPV="" Price_x0020_one_x0020_way_x002d_1="" Price_x0020_return_x002d_MPV_x00="" /> 
</Data>
</Root>

XSL

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" /> 
      <xsl:temp开发者_开发问答late match="/">
 <div id="locations">
 <ul id ="navbar" class="MSC_PrimaryNavFrame">
 <li><a href="#"><b>Going to Heathrow?<br />Find your fare fast!</b></a>
 <ul class="locations">
 <xsl:for-each select="//Data/Row">
 <li><a><xsl:attribute name ="href"><xsl:value-of  select="@Location"/>_toheathrow.aspx</xsl:attribute>
 <xsl:value-of select = "@Location" />
 </a>
 </li>
 </xsl:for-each>
 </ul>
 </li>
 </ul>
 </div></xsl:template>
 </xsl:stylesheet>

Sorry if I didn't post the code correctly-if I leave in line breaks it removes parts.


There are at least three different ways of doing this:

  1. Using AVT (Attribute-Value Templates) -- recommended

        <li>
            <a href="{@Location}toheathrow.aspx">
                <xsl:value-of select = "@Location" />
            </a>
        </li>
    
  2. Using the standard XPath function concat():

--

<a>
    <xsl:attribute name="href">
     <xsl:value-of select=
     "concat(@Location,'toheathrow.aspx')"/>
    </xsl:attribute>
    <xsl:value-of select = "@Location" />
</a>

.3. Using the Xslt instruction <xsl:text>:

--

<a>
    <xsl:attribute name="href">
     <xsl:value-of select="@Location"/>

     <xsl:text>toheathrow.aspx</xsl:text>
    </xsl:attribute>
    <xsl:value-of select = "@Location" />
</a>

.4. Also, in XSLT 2.0 one can use a select attribute on the <xsl:attribute> instruction:

<li>
    <a>
        <xsl:attribute name="href"
         select="concat(@Location, 'toheathrow.aspx')"/>
        <xsl:value-of select = "@Location" />
    </a>
</li>

I recommend using AVTs always when this is possible -- thus making the code shorter, simpler and more understandable.


Please do not post XML documents in one line. Provided XML (fragment, because there is no root) does not match to XSL stylesheet (Field empty elements with some attributes, but definitely no Location attribute here and //Data/Row path).

You provided just part of XSL with xsl:for-each loop. I guess that you are looking for something like:

<xsl:for-each select="//Data/Row">
    <li>
        <a href="{@Location}toheathrow.aspx">
            <xsl:value-of select="@Location"/>
        </a>
    </li>
</xsl:for-each>

If @Location attribute's value has spaces in itself, then you can additionally use normalize-space() function (from XPath 1.0). For example:

normalize-space('  some scattered    value   ') = 'some scattered value'

EDIT:

Changed {concat(@Location, 'toheathrow.aspx')} to {@Location}toheathrow.aspx. It's rightly more compact.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜