当我这样做时
account[@id=15]
我得到了
<?xml version="1.0" encoding="utf-8"?>
<root>
<account id="15" first_name="Sandra" last_name="Schlichting">
<private_address address_id="19" />
<profile_employee fk_id="15">
<date_created>2011-1-2T1:1:00</date_created>
<address building="3" room="2" floor="1" />
</profile_employee>
<profile_student fk_id="15">
<address address_id="19" />
</profile_student>
<profile_student fk_id="15">
<address address_id="45" />
</profile_student>
</account>
</root>
但我想输出
的值有人能弄明白该怎么做吗?
更新
这些命令有效
account[@id=15]/profile_employee
account[@id=15]/profile_employee/address
但输出整个元素,而不仅仅是属性房间和建筑物。
答案 0 :(得分:1)
你需要:
/root/account[@id=15]/@first_name
/root/account[@id=15]/@last_name
/root/account[@id=15]/profile_employee/address/@building
/root/account[@id=15]/profile_employee/address/@room
如果在XSLT中针对您的样本进行测试,结果将是(为了清晰起见,换行符):
Sandra
Schlichting
3
2
答案 1 :(得分:1)
虽然@ Flack的答案是正确的,但是可以使用单个XPath表达式生成想要的结果:
concat('
', /*/account[@id=15]/@first_name,
' ', /*/account[@id=15]/@last_name,
' : ', /*/account[@id=15]/profile_employee/address/@building,
'/', /*/account[@id=15]/profile_employee/address/@room
)
在提供的XML文档上评估此XPath表达式时:
<root>
<account id="15" first_name="Sandra" last_name="Schlichting">
<private_address address_id="19" />
<profile_employee fk_id="15">
<date_created>2011-1-2T1:1:00</date_created>
<address building="3" room="2" floor="1" />
</profile_employee>
<profile_student fk_id="15">
<address address_id="19" />
</profile_student>
<profile_student fk_id="15">
<address address_id="45" />
</profile_student>
</account>
</root>
产生了想要的正确结果:
Sandra Schlichting : 3/2