Reputation: 83
I query Microsoft Dynamics using fetchXML. They query is contstructed dynamically, by using an XML like syntax.
Simplified Example :
<fetch version="1.0" mapping="logical" page="1" count="5" returntotalrecordcount="true"><entity name="msdyn_bookingjournal"><attribute name="msdyn_name"/><attribute name="msdyn_starttime"/><attribute name="msdyn_endtime"/></entity></fetch>
The result is assigned to url parameter fetchXml and needs to be URL encoded. fetchXML=%3Cfetch%20version%3D%221.0%22%20mapping%3D%22logical%22%20page%3D%221%22%20count%3D%225%22%20returntotalrecordcount%3D%22true%22%3E%3Centity%20name%3D%22msdyn_bookingjournal%22%3E%3Cattribute%20name%3D%22msdyn_name%22%2F%3E%3Cattribute%20name%3D%22msdyn_starttime%22%2F%3E%3Cattribute%20name%3D%22msdyn_endtime%22%2F%3E%3C%2Fentity%3E%3C%2Ffetch%3E
I wish to do the URL encoding in XSLT using encode-for-uri, however the XML like structure is giving parsing errors when saving in Notepad++ : 'XML Parsing error at Line 15: Extra content at the end of the document' or xsltfiddle "xsl:value-of" must not contain the '<' character.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:hci="http://sap.com/it/"
exclude-result-prefixes="hci"
version="2.0">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<!-- https://enlightenedintegration.wordpress.com/2018/02/27/cpi-updating-message-headers-and-property-through-xslt/ -->
<!-- needed to call the functions for setting header and propert values -->
<xsl:param name="exchange"/>
<xsl:param name="MSDQuery"/>
<xsl:param name="MSDHost" />
<xsl:param name="query"/>
<xsl:variable name ="encodedQuery"><xsl:value-of select="encode-for-uri('<fetch version=1.0 mapping=logical page=1 count=10>' + $query + '</fetch>')"/></xsl:variable>
<xsl:template match="/">
<xsl:value-of select="hci:setProperty($exchange, 'MSDQuery', $encodedQuery)" />
</xsl:template>
</xsl:stylesheet>
How can this query be encoded by encode-for-uri?
I created a fiddle : https://xsltfiddle.liberty-development.net/ncntCRX
Note :
Upvotes: 0
Views: 335
Reputation: 1882
The error message is clear. This is not well-formed XML:
<xsl:value-of select="encode-for-uri('<fetch version=1.0 mapping=logical page=1 count=10>' + $query + '</fetch>')"/>
Using entities, this is well-formed XML:
<xsl:value-of select="encode-for-uri('<fetch version=1.0 mapping=logical page=1 count=10>' + $query + '</fetch>')"/>
Note: $query
should be a string, and it doesn't have to be "XML escaped" unless it's defined in an XML context.
Upvotes: 1