问题描述
所以我的系统中有一个员工的XML文档:
So I have an XML document of employees in my system:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<couriersystem title="System"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
<!-- snip -->
<employees>
<employee eid="1">
<nin>AZ123518D</nin>
<firstname>Peter</firstname>
<lastname>Smith</lastname>
<gender>Male</gender>
<dob>1994-02-11</dob>
<email>ps11@gmail.com</email>
<address>
119, London Street, Nidrie, F57 8NE
</address>
<tel>07005748900</tel>
<salary>30526</salary>
<empbranch bid="1" />
<supervisor sid="1" />
</employee>
<employee eid="2">
<nin>CN174869F</nin>
<firstname>Jennifer</firstname>
<lastname>Black</lastname>
<gender>Male</gender>
<dob>1984-12-24</dob>
<email>jb21@gmail.com</email>
<address>
161, South Road, Nidrie, W79 8WG
</address>
<tel>07555111222</tel>
<salary>40576</salary>
<empbranch bid="2" />
<supervisor sid="1" />
</employee>
<employee eid="3">
<nin>ET127654M</nin>
<firstname>Aaron</firstname>
<lastname>Jones</lastname>
<gender>Male</gender>
<dob>1968-03-15</dob>
<email>aj31@gmail.com</email>
<address>
66, High Road, Yoker, Q47 4SR
</address>
<tel>07856471267</tel>
<salary>30526</salary>
<empbranch bid="3" />
<supervisor sid="1" />
</employee>
<employee eid="4">
<nin>GC765238A</nin>
<firstname>Alistair</firstname>
<lastname>Smith</lastname>
<gender>Male</gender>
<dob>1976-11-26</dob>
<email>as11@gmail.com</email>
<address>
109, West Plaza, Clydebank, G55 8RC
</address>
<tel>07000123123</tel>
<salary>25400</salary>
<empbranch bid="4" />
<supervisor sid="1" />
</employee>
<employee eid="5">
<nin>HP146854D</nin>
<firstname>Emma</firstname>
<lastname>Reynolds</lastname>
<gender>Male</gender>
<dob>1995-05-05</dob>
<email>er11@yahoo.com</email>
<address>
57, Scott Street, Aberdeen, O75 2KS
</address>
<tel>07625361536</tel>
<salary>25400</salary>
<empbranch bid="5" />
<supervisor sid="7" />
</employee>
<!-- snip -->
</employees>
<!-- snip -->
</couriersystem>
我有以下XPath可以由其主管检索所有员工详细信息:
And I have the following XPath to retrieve all of the employee details by their supervisor:
//employee[supervisor/@sid='1']/concat('Name: ', concat(firstname/text(), ' ', lastname/text(), '\nGender: ', gender/text(), '\nD.O.B: ', dob/text()))
但是它只显示其余雇员中的一名雇员,并且还会显示\n
字符.
But it only shows one employee out of the rest of them, and the \n
characters show as well.
如何解决此问题?
推荐答案
要在XPath中的concat()
中解释\n
,请使用codepoints-to-string(10)
[credit::
To get \n
to be interpreted in concat()
in XPath, use codepoints-to-string(10)
[credit: @madcrazydrumma]:
//employee[supervisor/@sid='1']/concat('Name: ', firstname, ' ', lastname,
codepoints-to-string(10),
'Gender: ', gender,
codepoints-to-string(10),
'D.O.B: ', dob)
然后将返回以下结果:
Name: Peter Smith
Gender: Male
D.O.B: 1994-02-11
Name: Jennifer Black
Gender: Male
D.O.B: 1984-12-24
Name: Aaron Jones
Gender: Male
D.O.B: 1968-03-15
Name: Alistair Smith
Gender: Male
D.O.B: 1976-11-26
次要注意事项:事实证明,您的基本XPath 2.0表达式很好.正如我们在评论中发现的那样,由于网站的限制 xpathtester.com/xpath >.使用 videlibri.sourceforge.net/cgi-bin/xidelcgi 来进行在线XPath 2.0测试
Minor note: Your basic XPath 2.0 expression turned out to be fine. As we discovered in the comments, the results were incorrect due to limitations of the site, xpathtester.com/xpath. Use videlibri.sourceforge.net/cgi-bin/xidelcgi instead for online XPath 2.0 testing.
这篇关于通过XPath中的concat()换行(\ n)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!