删除 xsl 中的空白
如何消除在破坏链接的“位置”元素之后添加的空格? 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:template 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>
抱歉,如果我没有正确发布代码 - 如果我留下换行符,它会删除部分内容。
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:template 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少有三种不同的方法可以实现此目的:
使用 AVT (属性值模板) -- 推荐
<前><代码>
使用标准 XPath 函数
concat()
:--
.3。 使用 Xslt 指令
:--
.4。 此外,在 XSLT 2.0 中,可以在
指令上使用select
属性:我建议始终使用 AVT当这成为可能时——从而使代码更短、更简单、更容易理解。
There are at least three different ways of doing this:
Using AVT (Attribute-Value Templates) -- recommended
Using the standard XPath function
concat()
:--
.3. Using the Xslt instruction
<xsl:text>
:--
.4. Also, in XSLT 2.0 one can use a
select
attribute on the<xsl:attribute>
instruction:I recommend using AVTs always when this is possible -- thus making the code shorter, simpler and more understandable.
请不要在一行中发布 XML 文档。提供的 XML(片段,因为没有根)与 XSL 样式表不匹配(带有某些属性的Field
空元素,但这里绝对没有Location
属性和/ /Data/Row
路径)。您仅使用您正在寻找类似的内容:xsl:for-each
循环提供了 XSL 的一部分。我猜如果
@Location
属性的值本身有空格,那么您还可以使用normalize-space()
函数(来自 XPath 1.0)。例如:编辑:
将
{concat(@Location, 'toheathrow.aspx')}
更改为{@Location}toheathro.aspx
。它确实更紧凑。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 noLocation
attribute here and//Data/Row
path).You provided just part of XSL withyou are looking for something like:xsl:for-each
loop. I guess thatIf
@Location
attribute's value has spaces in itself, then you can additionally usenormalize-space()
function (from XPath 1.0). For example:EDIT:
Changed
{concat(@Location, 'toheathrow.aspx')}
to{@Location}toheathrow.aspx
. It's rightly more compact.