文本節點註冊前綴的綁定到相應的命名空間之後的所有元素,使用:
/*/s:Body
/s:About_ServiceResponse
/s:About_ServiceResult
/a:businessServiceVersionStructureField
/a:BusinessServiceVersionStructureType
/a:businessServiceVersionNameField
/text()
當此XPath表達式針對fo llowing XML文檔(所提供的一個嚴重畸形的,我不得不花費大量的時間,使之充分形成):
<s:Envelope xmlns:s="http://...">
<s:Body>
<About_ServiceResponse xmlns="http://...">
<About_ServiceResult xmlns:a="http://">
<a:businessServiceVersionStructureField>
<a:BusinessServiceVersionStructureType>
<a:businessServiceDBVersionNameField>V001</a:businessServiceDBVersionNameField>
<a:businessServiceVersionNameField>Some Service^V100</a:businessServiceVersionNameField>
</a:BusinessServiceVersionStructureType>
</a:businessServiceVersionStructureField>
</About_ServiceResult>
</About_ServiceResponse>
</s:Body>
</s:Envelope>
究竟想要的文本節點選擇:
Some Service^V100
如果要選擇作爲此文本節點父級的元素,請使用:
/*/s:Body
/s:About_ServiceResponse
/s:About_ServiceResult
/a:businessServiceVersionStructureField
/a:BusinessServiceVersionStructureType
/a:businessServiceVersionNameField
XSLT - 基於驗證:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://..." xmlns:a="http://">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/s:Body
/s:About_ServiceResponse
/s:About_ServiceResult
/a:businessServiceVersionStructureField
/a:BusinessServiceVersionStructureType
/a:businessServiceVersionNameField
/text()
"/>
=======
<xsl:copy-of select=
"/*/s:Body
/s:About_ServiceResponse
/s:About_ServiceResult
/a:businessServiceVersionStructureField
/a:BusinessServiceVersionStructureType
/a:businessServiceVersionNameField
"/>
</xsl:template>
</xsl:stylesheet>
當該變換被針對同一XML文檔(以上),所選擇的節點是輸出(使用施加 「=======」 作爲分隔符):
Some Service^V100
=======
<a:businessServiceVersionNameField xmlns:a="http://" xmlns="http://..." xmlns:s="http://...">Some Service^V100</a:businessServiceVersionNameField>
「我嘗試過使用XPath,但無法使其工作。」你嘗試了什麼XPath表達式,結果如何? – LarsH