how to get text with xpath from bad xml?
I have a "bad xml structure" file:
<cars>
<car>Toyota
<country>Japan</coutry>
....
</car>
</cars>
How 开发者_开发问答to correctly get the right word (Toyota) using Xpath? I tried:
<xsl:value-of select = "cars/car/text()"/>
.
It works, but I think there are more appropriate methods. Thanks.
Use:
/cars/car/text()[1]
or if you want to discard most of the white space in the text node selected above, use:
normalize-space(/cars/car/text()[1])
Do note that while in XSLT 1.0 <xsl:value-of>
outputs the string valu only of the first node of the node-set selected by the expression in the select
attribute, <xsl:copy-of>
will output all the nodes in the node-set. In XSLT 2.0 even <xsl:value-of>
outputs all the nodes in the node-set.
Therefore, for purposes of portability, upgradability and simply for avoiding errors, it is better to specify which exactlyy node from the nodeset is to be output -- even when using <xsl:value-of>
精彩评论