【问题标题】:XSL how to check for commas in a url?XSL如何检查网址中的逗号?
【发布时间】:2012-05-25 20:29:11
【问题描述】:
<xsl:choose>
 <xsl:when test="./link/@href ">
  <td class="link"> <a href="{@href}"> 
    <xsl:copy-of select="./link/@href"/>Link</a> </td>
 </xsl:when>
 <xsl:otherwise>
  <td class="nolink"> <a href="{@href}"> 
   <xsl:copy-of select="./link/@href"/>Link</a> </td>
 </xsl:otherwise>
</xsl:choose>

鉴于以下代码,我想添加另一个测试以查看我的 href-url 链接中是否包含一个或多个逗号。如果这是真的,我想为 td 分配一个不同的类。也许检查没有逗号会更好。 我想过“包含”,但我不知道如何为此设置结构。

感谢任何想法。

【问题讨论】:

  • 为什么 URL 中会有逗号?为什么有逗号意味着它需要一个不同的类?为什么要在 XSLT 中而不是在浏览器中执行此操作,在浏览器中您可能会使用诸如 td a[href*=","] { ... } 之类的 CSS 规则?上面的代码也被分解得很差(实际上根本没有)。
  • 这是一个程序的特殊 url。因此,其中有一个逗号意味着两个链接指向两个不同的“文档”。如果没有逗号 - 它只是一个链接。如果网址中有两个或多个链接,我只想显示一个不同的图标。与我在这里的方法相比,我很高兴看到一种更好的方法。
  • 无论 XML 来自上游的何处,将多个链接放入单个 @href 属性是一种糟糕的设计,这会迫使您在 XSL 中识别它们并自己解析它们。我建议生成 XML,以便每个链接都有自己的 link 元素。然后,您可以使用 count(link)&gt;1 之类的东西进行检查,如果您希望能够找到具有多个链接的条目以便在视觉上区分它们或其他内容。

标签: xml string url xslt contains


【解决方案1】:

使用 xpath 函数 contains 将是可行的方法,但解决此问题的一种方法是使用模板进行匹配,而不是使用 xsl:choose你想要的案例。因此,您首先要查找任何 link 元素

<xsl:apply-templates select="link"/>

然后您将有一个模板来匹配 @href 属性包含一个命令的 link 元素

<xsl:template match="link[contains(@href, ',')]">

您还需要一个更通用的模板来匹配所有其他 link 元素。只有在其他更具体的模板没有找到匹配项时才会匹配

<xsl:template match="link">

例如,考虑以下 XML

<root>
   <doc>
      <link href="http://www.example.com" />
   </doc>
   <doc>
      <link href="http://www.example1.com,http://www.example2.com" />
   </doc>
</root>

当您应用以下 XSLT 时

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>

   <xsl:template match="root">
      <table>
         <tr>
            <xsl:apply-templates select="doc"/>
         </tr>
      </table>
   </xsl:template>

   <xsl:template match="doc">
      <xsl:apply-templates select="link"/>
      <xsl:if test="not(link[@href])">
         <td>No link</td>
      </xsl:if>
   </xsl:template>

   <xsl:template match="link[contains(@href, ',')]">
      <td class="link2">
         <xsl:call-template name="link">
            <xsl:with-param name="href" select="substring-before(@href, ',')"/>
         </xsl:call-template>
         <xsl:call-template name="link">
            <xsl:with-param name="href" select="substring-after(@href, ',')"/>
         </xsl:call-template>
      </td>
   </xsl:template>

   <xsl:template match="link">
      <td class="link">
         <xsl:call-template name="link"/>
      </td>
   </xsl:template>

   <xsl:template name="link">
      <xsl:param name="href" select="@href"/>
      <a href="{$href}">
         <xsl:copy-of select="$href"/> Link</a>
   </xsl:template>
</xsl:stylesheet>

那么下面是输出

<table>
   <tr>
      <td class="link">
         <a href="http://www.example.com"> Link</a>
      </td>
      <td class="link2">
         <a href="http://www.example1.com">http://www.example1.com Link</a>
         <a href="http://www.example2.com">http://www.example2.com Link</a>
      </td>
   </tr>
</table>

注意,这也使用了命名模板来避免重复代码。

编辑:如果您有多个元素,以及具有 @href 属性的 link,那么您可以通过多种方式来做到这一点。如果你的名字数量有限,你可以这样做

<xsl:apply-templates select="link|website|localpath" />

然后你可以像这样匹配它们......

<xsl:template match="link[contains(@href, ',')]|website[contains(@href, ',')]|localpath[contains(@href, ',')]">

<xsl:template match="link|website|website" />

最好是查看任何元素,像这样

<xsl:apply-templates select="*" />

然后匹配任何带有 href 属性的元素,以及那些没有的

<xsl:template match="doc/*[contains(@href, ',')]">

<xsl:template match="doc/*">

【讨论】:

  • 感谢您提供详细信息。一个问题。如果有不同的名称包含 href-attributes 而不仅仅是 ,我需要如何编辑它?示例: |
  • 我已经扩展了我的答案,希望能回答这个问题!
【解决方案2】:

你正在寻找这样的东西

<xsl:if test=".[contains(@href,',')]">
  <xsl:attribute name="class">myclass</xsl:attribute>
</xsl:if>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多