Getting "prefix 'soap' is not defined" in xslt file
I am trying to transform a string with xml data (response from a web service). I tried to start simple by just getting the name:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<table>
<tr>
<th>Name</th>
</tr>
<xsl:for-each select="soap:Envelope/soap:Body/ABRSearchByABNResponse/ABRPayloadSearchResults/response/legalName/">
<tr>
<td>
<xsl:value-of select="givenName"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xs开发者_如何转开发l:stylesheet>
However, I get "prefix 'soap' is not defined", how do I fix this? Thanks.
In XSLT any namespace prefix used in an XPath expression must be defined in a corresponding namespace declaration.
This is not the case with your code and hence the error.
Solution:
Declare the soap namespace:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:soap="http://soap/envelope/"
>
The namespace prefix, soap
in your case, is just shorthand for the real namespace URI. To avoid you have http://soap/envelope/
everywhere, you can define once that soap
stands for http://soap/envelope/
, and use soap
in the rest of your document.
This means that if you use a namespace prefix, it has to be defined so that the real namespace can be found.
You could also declare that pizza
corresponds to http://soap/envelope/
and use that instead. The soap
namespace prefix is not special.
精彩评论