English 中文(简体)
Checking the starting character should be T and next 3 character should be number in xslt
原标题:
  • 时间:2010-09-05 06:44:37
  •  标签:
  • xslt
  • tridion

I am using XSLT1.0 for transforming my XML.

I am having below Tabs.xml

<?xml version="1.0"?>
<tcm:ListItems xmlns:tcm="http://www.tridion.com/ContentManager/5.0" ID="tcm:481-87289-4" Managed="68">
  <tcm:Item ID="tcm:481-596728-64" Title="T000. Brisbane" pageURL="/australia/brisbane/index.aspx" componentTitle="Overview"/>
  <tcm:Item ID="tcm:481-598671-64" Title="Tabs XML"/>
  <tcm:Item ID="tcm:481-598672-64" Title="T030 Special Offers" pageURL="/australia/brisbane/specialoffers.aspx" componentTitle="Special Offers"/>
  <tcm:Item ID="tcm:481-598673-64" Title="020 Flight Schedules" pageURL="/australia/brisbane/flightschedules.aspx" componentTitle="Flight Schedules"/>
  <tcm:Item ID="tcm:481-598674-64" Title="T010 Guide" pageURL="/australia/brisbane/guide.aspx" componentTitle="Guide"/>
</tcm:ListItems>

And I am using below xslt to transform it!

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:em="http://www.emirates.com/tridion/schemas" xmlns:tcmse="http://www.tridion.com/ContentManager/5.1/TcmScriptAssistant" exclude-result-prefixes="em xlink tcmse tcm">
  <xsl:output method="xml" version="1.0" encoding="UTF-16" indent="yes"/>
  <xsl:template match="tcm:ListItems">
    <list type="Tabs">
      <xsl:apply-templates select="tcm:Item">
        <xsl:sort select="@Title" order="ascending"/>
      </xsl:apply-templates>
    </list>
  </xsl:template>
  <!-- add field values for each item-->
  <xsl:template match="tcm:Item">
    <xsl:if test="@componentTitle !=   ">
      <xsl:element name="tab">
        <xsl:attribute name="id">
          <xsl:value-of select="substring-after(@ID,  - )"/>
        </xsl:attribute>
        <xsl:attribute name="title">
          <xsl:value-of select="@componentTitle"/>
        </xsl:attribute>
        <xsl:attribute name="url">
          <xsl:value-of select="@pageURL"/>
        </xsl:attribute>
      </xsl:element>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Previously, it was working fine, but now there is another change required, now I need those node to be rendered whose @Title starts with T or t as well as next 3 character should be number for example in above xml "Flight Schedule" shouldn t come, I assume i just need to write one more and condition in <xsl:if test="@componentTitle != ">,

Please suggest! how it can be done!

最佳回答

I came up with below solution, please suggest if it is OK!

<xsl:template match="tcm:Item">
    <xsl:if test="@componentTitle !=    and (starts-with(translate(@Title,  t ,  T ),  T )and string(number(substring(@Title,2,3))) !=  NaN )">
      <xsl:element name="tab">
        <xsl:attribute name="id">
          <xsl:value-of select="substring-after(@ID,  - )"/>
        </xsl:attribute>
        <xsl:attribute name="title">
          <xsl:value-of select="@componentTitle"/>
        </xsl:attribute>
        <xsl:attribute name="url">
          <xsl:value-of select="@pageURL"/>
        </xsl:attribute>
      </xsl:element>
    </xsl:if>
  </xsl:template>

Your all input is required!

问题回答

Here is the simplest expression that will do the matching:

translate(substring(@Title, 1, 4),  t12345679 ,  T000000000 ) =  T000 

Here is the template:

<xsl:template match="
 tcm:Item[translate(substring(@Title, 1, 4),  t12345679 ,  T000000000 ) =  T000 
 and @componentTitle !=    "/>

The selection logic is best put into the match pattern of the template -- and it can be a little simplified, too.

<xsl:template match=
 "tcm:Item[contains( tT , substring(@Title,1,1))
         and
           number(substring(@Title,2,3)) 
          =    
           number(substring(@Title,2,3)) 
          ]
 ">

One rule to remember: Always test if some string $s represents a number -- like this:

number($s) = number($s)

You should select the nodes you want to operate on in your templates in the apply-templates element:

<xsl:apply-templates select="tcm:Item[@componentTitle !=    
                           and (starts-with(translate(@Title,  t ,  T ),  T ) 
                           and string(number(substring(@Title,2,3))) !=  NaN )]">

I have used the test that @solution produced for this - it works well for the requirement (tested here).

This is better than selecting all tcm:Item nodes and testing them one at a time in the template.

Working with XSLT only very occasionally, I came up with the following solution:

<xsl:if test="@componentTitle !=   ">
  <xsl:if test="starts-with(@Title, T ) or starts-with(@Title, t )"> 
    <xsl:if test="string-length(@Title) &gt; 3">
      <xsl:if test="string(number(substring(@Title,2,3))) !=  NaN "> 
...
      </xsl:if>
    </xsl:if>
  </xsl:if>
</xsl:if>

I noticed that there are also functions lower-case/upper-case which could be used for testing for the first character being a t or T but they seem to be only available in XSLT2.0 .





相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签