English 中文(简体)
XSL 如何检查 URL 中的逗号?
原标题:XSL how to check for commas in a url?
<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>

Given the following code, I want to add another test to see whether my href-url link has got one or more commas in it. If that s true I want to assign a different class to the td. Maybe checking for no comma would be even better. I thought about "contains" but I don t know how to set up the structure for that.

任何想法都感激不尽。

最佳回答

使用 xpath 函数 < 坚固 > 包含 < / 坚固 >, 将会是前进的方向, 但一种办法就是, 使用模板来匹配您想要的大小写。 因此, 您首先寻找任何 < 坚固 > 链接 < / 坚固 > 的元素 。

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

然后您将有一个模板来匹配 < strong> link 元素, 其中 @href 属性包含逗号 。

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

您还需要一个更通用的模板, 以匹配所有其它 < command> > 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>

请注意,本节还使用一个命名模板,以避免代码重复。

EDIT: 如果您有多个元素, 以及有 @href 属性的 < strong > 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/*">
问题回答

你在找这样的东西

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




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签