XSLT 选择包含特定子字符串的所有节点 [英] XSLT Select all nodes containing a specific substring

查看:23
本文介绍了XSLT 选择包含特定子字符串的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 XPath,它将选择包含特定单词的某些节点.在这种情况下,这个词是Lockwood".正确答案是 3.这两条路径都给我 3.

I'm trying to write an XPath that will select certain nodes that contain a specific word. In this case the word is, "Lockwood". The correct answer is 3. Both of these paths give me 3.

count(//*[contains(./*,'Lockwood')])
count(BusinessLetter/*[contains(../*,'Lockwood')])

但是当我尝试输出每个特定节点的文本时

But when I try to output the text of each specific node

//*[contains(./*,'Lockwood')][1]
//*[contains(./*,'Lockwood')][2]
//*[contains(./*,'Lockwood')][3]

节点 1 最终包含所有文本,节点 2 和节点 3 为空.

Node 1 ends up containing all the text and nodes 2 and 3 are blank.

有人可以告诉我发生了什么或我做错了什么.

Can some one please tell me what's happening or what I'm doing wrong.

谢谢.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XPathFunctions.xsl"?>
<BusinessLetter>
 <Head>
  <SendDate>November 29, 2005</SendDate>
  <Recipient>
   <Name Title="Mr.">
    <FirstName>Joshua</FirstName>
    <LastName>Lockwood</LastName>
   </Name>
   <Company>Lockwood &amp; Lockwood</Company>
   <Address>
    <Street>291 Broadway Ave.</Street>
    <City>New York</City>
    <State>NY</State>
    <Zip>10007</Zip>
    <Country>United States</Country>
   </Address>
  </Recipient>
 </Head>
 <Body>
  <List>
   <Heading>Along with this letter, I have enclosed the following items:</Heading>
   <ListItem>two original, execution copies of the Webucator Master Services Agreement</ListItem>
   <ListItem>two original, execution copies of the Webucator Premier Support for Developers Services Description between Lockwood &amp; Lockwood and Webucator, Inc.</ListItem>
  </List>
  <Para>Please sign and return all four original, execution copies to me at your earliest convenience.  Upon receipt of the executed copies, we will immediately return a fully executed, original copy of both agreements to you.</Para>
  <Para>Please send all four original, execution copies to my attention as follows:

 <Person>
    <Name>
     <FirstName>Bill</FirstName>
     <LastName>Smith</LastName>
    </Name>
    <Address>
     <Company>Webucator, Inc.</Company>
     <Street>4933 Jamesville Rd.</Street>
     <City>Jamesville</City>
     <State>NY</State>
     <Zip>13078</Zip>
     <Country>USA</Country>
    </Address>
   </Person>
  </Para>
  <Para>If you have any questions, feel free to call me at <Phone>800-555-1000 x123</Phone> or e-mail me at <Email>bsmith@webucator.com</Email>.</Para>
 </Body>
 <Foot>
  <Closing>
   <Name>
    <FirstName>Bill</FirstName>
    <LastName>Smith</LastName>
   </Name>
   <JobTitle>VP of Operations</JobTitle>
  </Closing>
 </Foot>
</BusinessLetter>

推荐答案

但是当我尝试输出每个特定节点

But when I try to output the text of each specific node

//*[contains(./*,'Lockwood')][1] 
//*[contains(./*,'Lockwood')][2] 
//*[contains(./*,'Lockwood')][3] 

节点 1 最终包含所有文本节点 2 和 3 为空

Node 1 ends up containing all the text and nodes 2 and 3 are blank

这是一个常见问题.

//SomeExpression[1]

不等同于

(//someExpression)[1]

前者选择所有 //SomeExpression 节点,它们是其父节点的第一个子节点.

The former selects all //SomeExpression nodes that are the first child of their parent.

后者选择整个文档中所有//SomeExpression节点的第一个(按文档顺序).

The latter selects the first (in document order) of all //SomeExpression nodes in the whole document.

这如何适用于您的问题?

//*[contains(./*,'Lockwood')][1]

这将选择具有至少一个字符串值包含Lockwood"的子元素并且是其父元素的第一个这样的子元素的所有元素.具有包含字符串Lockwood"的文本节点的所有三个元素都是其父元素的第一个这样的子元素,因此结果是选择了三个元素.

This selects all elements that have at least one child whose string value contains 'Lockwood' and that are the first such child of their parent. All three elements that have a text node containing the string 'Lockwood' are the first such child of their parents, so the result is that three elements are selected.

//*[contains(./*,'Lockwood')][2]

没有任何元素具有包含字符串Lockwood"的字符串值的子元素,并且是其父元素的第二个这样的子元素.未选择任何节点.

There is no element that has a child with string value containing the string 'Lockwood' and is the second such child of its parent. No nodes are selected.

//*[contains(./*,'Lockwood')][3]

没有任何元素具有包含字符串Lockwood"的字符串值的子元素,并且是其父元素的第三个这样的子元素.未选择任何节点.

There is no element that has a child with string value containing the string 'Lockwood' and is the third such child of its parent. No nodes are selected.

解决方案:

使用:

(//*[contains(./*,'Lockwood')])[1]
(//*[contains(./*,'Lockwood')])[2]
(//*[contains(./*,'Lockwood')])[3]

每一个都精确地选择由 //*[contains(./*,'Lockwood')] 选择的第 N 个元素 (N = {1,2,3}),相应地: BusinesLetterRecipientBody.

Each of these selects exactly the Nth element (N = {1,2,3}) selected by //*[contains(./*,'Lockwood')], correspondingly: BusinesLetter, Recipient and Body.

记住:

[] 运算符的优先级(优先级)高于缩写 //.

The [] operator has higher priority (precedence) than the // abbreviation.

这篇关于XSLT 选择包含特定子字符串的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆