删除 xsl 中的空白

发布于 2024-11-15 15:45:56 字数 2694 浏览 2 评论 0原文

如何消除在破坏链接的“位置”元素之后添加的空格? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-11-22 15:45:56

至少有三种不同的方法可以实现此目的

  1. 使用 AVT (属性值模板) -- 推荐

    <前><代码>

  2. ;


  3. 使用标准 XPath 函数concat()

--

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

.3。 使用 Xslt 指令:

--

<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。 此外,在 XSLT 2.0 中,可以在 指令上使用 select 属性

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

我建议始终使用 AVT当这成为可能时——从而使代码更短、更简单、更容易理解。

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.

许仙没带伞 2024-11-22 15:45:56

请不要在一行中发布 XML 文档。提供的 XML(片段,因为没有根)与 XSL 样式表不匹配(带有某些属性的 Field 空元素,但这里绝对没有 Location 属性和 / /Data/Row 路径)。

您仅使用 xsl:for-each 循环提供了 XSL 的一部分。我猜您正在寻找类似的内容:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文