2011-06-13 185 views
2

如何删除在打破链接的“Location”元素后添加的空格? IE: “/位置toxxx.aspx” XML删除xsl中的空白

- <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> 

很抱歉,如果我没有贴码正确,如果我留在换行符它删除部分。

+0

好问题,+1。查看我对三种不同XSLT 1的回答。0解决方案和第四个仅在XSLT 2.0中可用的解决方案。我建议在可能的情况下始终使用AVT。 – 2011-06-14 02:17:41

+0

不好的问题-1!您发布的代码不会添加您抱怨的空白。我得到的印象是你的真实代码是不同的,并且在“toHeathrow”字符串旁边有空白 - 但你已经让我猜测了。 – 2011-06-14 07:55:24

+0

@Michael Kay:我认为@matt在编写代码时遇到困难。当我读到这个问题时,在“toHeathrow.aspx”之后有一个回车符和空格 - 可能是我自己的IDE重新格式化的结果。 – 2011-06-14 12:50:19

回答

4

有这样做的至少三种不同的方式:

  1. 使用AVT(Attribute-Value Templates) - 推荐

    <li> 
         <a href="{@Location}toheathrow.aspx"> 
          <xsl:value-of select = "@Location" /> 
         </a> 
        </li> 
    
  2. 使用标准的XPath功能concat()

-

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

0.3。 使用XSLT指令<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> 

0.4。 此外,在XSLT 2.0可以在<xsl:attribute>指令使用select属性:

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

我建议使用AVTS一直当这是可能的 - 从而使代码更短,更简单,更容易理解。

+0

这完美的作品!非常感谢。 – matt 2011-06-14 15:06:38

+0

@matt:不客气。 – 2011-06-14 17:10:13

2

请不要在一行中发布XML文档。提供的XML(片段,因为没有根目录)与XSL样式表不匹配( Field具有某些属性的空元素,但在此处肯定没有 Location属性,而 //Data/Row路径)。

您提供了仅与XSL的一部分 xsl:for-each循环。我想这 你正在寻找的东西,如:

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

如果@Location属性的值有空格本身,那么你还可以使用normalize-space()功能(从XPath 1.0中)。例如:

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

编辑:

更改{concat(@Location, 'toheathrow.aspx')}{@Location}toheathrow.aspx。它更加紧凑。

+0

感谢您的帮助。 AVT建议起作用。 – matt 2011-06-14 15:22:57