XSLT substring-after

时间:2016-07-29 11:27:12

标签: xslt-2.0

我有字符串:$number=Twentyone one two three。我需要在one的字符串two three之后获取值。我写了下面的xslt查询:

substring-after($number,'one') 

返回one two three

仅将整个字符串与one完全匹配然后返回two three的查询是什么?

3 个答案:

答案 0 :(得分:0)

如果您正在使用XSLT 2.0处理器(例如已标记的问题),那么您应该可以使用unit MainForm1; interface uses ..., ShellAPI; type eHelperState = (Idle, UpdatesAvailable, UpdatesDownloading); MainForm = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private TaskbarCreatedMsg: UINT; IconData: NOTIFYICONDATA; IconAdded: Boolean; ssHelperState: eHelperState; procedure SysTrayIconMessageHandler(var Message: TMessage); procedure AddSysTrayIcon; procedure ShowBalloonTips; procedure DeleteSysTrayIcon; procedures SetHelperState(NewState: eHelperState); ... end; var MainForm: TForm; implementation const TRAY_CALLBACK = WM_USER + $7258; {$IF RTLVersion < 21} NOTIFYICON_VERSION_4 = 4; {$IFEND} procedure MainForm.FormCreate(Sender: TObject); begin TaskbarCreatedMsg := RegisterWindowMessage('TaskbarCreated'); IconData.cbSize := SizeOf(IconData); IconData.Wnd := AllocateHWnd(SysTrayIconMessageHandler); IconData.uID := 1; AddSysTrayIcon; end; procedure MainForm.FormDestroy(Sender: TObject); begin DeleteSysTrayIcon; DeallocateHWnd(IconData.Wnd); end; procedure MainForm.AddSysTrayIcon; begin IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP; IconData.uCallbackMessage := TRAY_CALLBACK; IconData.hIcon := Application.Icon.Handle; StrLCopy(IconData.szTip, 'Software Updater is running', Length(IconData.szTip)); IconAdded := Shell_NotifyIcon(NIM_ADD, @IconData); if not IconAdded then begin ShowMessage('Unable to add System Tray Icon.'); Exit; end; if CheckWin32Version(5, 0) then begin IconData.{$IF RTLVersion >= 21}uVersion{$ELSE}uTimeout{$IFEND} := NOTIFYICON_VERSION_4; if not Shell_NotifyIcon(NIM_SETVERSION, @IconData) then ShowMessage('Unable to set version for System Tray Icon.'); end; end; procedure MainForm.DisplayBalloonTips; var Tip, InfoText, InfoTitle: string; begin if not IconAdded then Exit; case ssHelperState of UpdatesAvailable: begin Tip := 'Updates are Available. Click to see details.'; InfoText := 'Updates are available to the programs installed on your Computer. Click to see details.'; InfoTitle := 'Updates are Available'; end; UpdatesDownloading: begin Tip := 'Downloading Updates. Click to see details.'; InfoText := 'Updates are downloading in the background. Click to see details.'; InfoTitle := 'Downloading Updates'; end; else Tip := 'Software Updater is running'; end; IconData.uFlags := NIF_TIP or NIF_INFO; StrPLCopy(IconData.szTip, Tip, Length(IconData.szTip)); StrPLCopy(IconData.szInfo, InfoText, Length(IconData.szInfo)); StrPLCopy(IconData.szInfoTitle, InfoTitle, Length(IconData.szInfoTitle)); IconData.uTimeout := 2500; IconData.dwInfoFlags := NIIF_INFO; if not Shell_NotifyIcon(NIM_MODIFY, @IconData) then ShowMessage('Unable to update System Tray Icon.') end; procedure MainForm.DeleteSysTrayIcon; begin if IconAdded then begin IconAdded := False; if not Shell_NotifyIcon(NIM_DELETE, @IconData) then ShowMessage('Unable to delete System Tray Icon.'); end; end; procedures MainForm.SetHelperState(NewState: eHelperState); begin if ssHelperState <> NewState then begin ssHelperState := NewState; DisplayBalloonTips; end; end; procedure MainForm.SysTrayIconMessageHandler(var Message: TMessage); begin if Message.Msg = TRAY_CALLBACK then begin case LOWORD(Message.LParam) of WM_MOUSEMOVE: begin //... end; WM_LBUTTONDBLCLK, NIN_BALLOONUSERCLICK: begin // display status window... end; WM_CONTEXTMENU, NIN_KEYSELECT, NIN_SELECT: begin // display popup menu at coordinates specified by Msg.WParam... end; NIN_BALLOONSHOW:; NIN_BALLOONHIDE:; NIN_BALLOONTIMEOUT:; end; end else if (Message.Msg = TaskbarCreatedMsg) and (TaskbarCreatedMsg <> 0) then begin IconAdded := False; AddSysTrayIcon; DisplayBalloonTips; end else begin Message.Result := DefWindowProc(IconData.Wnd, Message.Msg, Message.WParam, Message.LParam); end; end; ... end. ...

轻松完成此操作
replace()

答案 1 :(得分:0)

此XPath 2.0表达式

for $vS in 'Twentyone one two three'
       return
         codepoints-to-string(
            reverse(
                string-to-codepoints(
                      substring-before(
                           codepoints-to-string(
                                 reverse(string-to-codepoints($vS))
                                                ), 
                           'eno')
                                     )
                    )
                               )

当评估产生字符串

two three

<强>解释

问题是在原始字符串中字符串“one”出现 last 之后找到子字符串。

一种解决方案是在反向原始字符串中找到字符串“eno”(“one”的反向)之前的子字符串 - 然后反转结果。

在XPath 2.0中,有一个标准函数reverse(),它从一个序列生成另一个序列,它是反转的原始序列。还有一个标准的XPath 2.0函数string-to-codepoints(),它从一个字符串产生一系列包含该字符串的代码点(比如Unicode“字符代码”)。相反,标准XPath 2.0函数codepoints-to-string()采用一系列有效的Unicode代码点(整数)并生成由这些代码点组成的字符串。

因此,产生字符串$vS反向的XPath 2.0表达式为:

codepoints-to-string(reverse(string-to-codepoints($vS)))

使用这些知识,我们可以构建上面的XPath 2.0表达式,它提供了所需的解决方案。

更为一般

要在$vS中最后一次出现$vS2之后查找 codepoints-to-string( reverse( string-to-codepoints( substring-before( codepoints-to-string( reverse(string-to-codepoints($vS)) ), codepoints-to-string(reverse(string-to-codepoints($vS2)))) ) ) ) 的子字符串,请使用此XPath 2.0表达式:

for $vS in 'Twentyone one two three'
   return
     tokenize($vS, '\W')[position() > index-of(tokenize($vS, '\W'), 'one')[last()]]

<强> II。替代解决方案 - 如果只是在最后一次出现字符串后需要单词

如果在最后一次出现特定字符串(“one”)之后只需要序列的尾随单词 - 没有精确的分隔符,则可以使用此XPath 2.0表达式:

for $vS in 'Twentyone one two three'
   return
     string-join(
       tokenize($vS, '\W')[position() > index-of(tokenize($vS, '\W'), 'one')[last()]],
       ' ')

如果您不需要序列的尾随单词,但是它们的串联(使用标准分隔符,例如空格),请使用:

<a href="file:///C:/UsersThatr/Pictures/9.jpg">9.jpg</a>

答案 2 :(得分:0)

这完全取决于您对输入字符串的了解程度。如果您事先知道它包含$number=Twentyone one two three,那么您已经知道答案是two three,因此您无需进行任何匹配。但大概你不知道。但你必须知道一些事情,我们无法在不知道什么的情况下解决问题。 Dimitre假设您在输入中最后一次出现“one”之后想要字符串,但我无法在您的问题中看到任何内容来证明该推断是正确的。如果输入是“二十一块骨头”,你想要什么?你可能(据我们所知)在第一次出现被空格包围的“one”之后寻找字符串。或者对于第一个单词后面的字符串,即单个数字的英文名称,或者第一个单词后面的字符串,即三个字符长。我们根本就不知道,你不能指望我们猜测。