You are viewing limited content. For full access, please sign in.

Question

Question

How do I escape an ampersand in the Read XML activity?

asked on June 14, 2016

I'm trying to use the Read XML activity to parse the following XML:

<Report Entity="Thing 1 & Thing 2" Name="Event Code Report">
    <Parameters>
        <Parameter Name="StartDate" Value="5/1/2012"  />
        <Parameter Name="EndDate" Value="5/31/2012"  />
        <Parameter Name="Fund" Value="[null]"  />
        <Parameter Name="ReportList" Value="Thing 1 & Thing 2"  />
        <Parameter Name="EventCode" Value="ISSUED"  />
    </Parameters>
</Report>

 

I create XPath Expression tokens, like //Parameter[@Name='ReportList']/@Value, but it doesn't like the ampersand and gives a warning of:

The value of the XML Source property is invalid. An error occurred while parsing EntityName. Line 8, position 53.

 

I tried adding the Encode XML function to the XML Source for the activity, and that gives a different error: 

The value of the XML Source property is invalid. Data at the root level is invalid. Line 1, position 1.

 

Other xml files that I parse, not containing an ampersand, create the tokens properly and without error. 

 

Is there a way I can handle this poorly formatted XML documents and extract the value successfully?

0 0

Answer

SELECTED ANSWER
replied on June 15, 2016

The results file is not a token we can run substitution on.

Your only option would be to write a script to download the e-doc yourself, run the replacement and generate a token you can feed to Read XML.

Be aware that & is allowed in XML as part of escaping of other reserved characters, including ampersand itself, so you would have to do something more than just replace all ampersands with &amp;

1 0

Replies

replied on June 14, 2016

The unescaped ampersand in the text means that it does not represent a legal XML document and the parser is correct to reject it as input.  Ideally, you could change the process that produces this output to escape "&" as "&amp;".  If that's not possible you could try doing a string replace prior to processing it as XML.  This is tricky to do if some entities are correctly escaped and only some are problematic.

1 0
replied on June 14, 2016

Agreed, however the source is outside our control. 

How would I go about "doing a string replace prior to processing it as XML" in my workflow?

0 0
replied on June 15, 2016

What are you using as the source file in Read XML?

0 0
replied on June 15, 2016 Show version history

I have the Download XML task pointed at an entry in the repository. 

 

Then the Read XML activity is using %(DownloadXML_Result File)

0 0
SELECTED ANSWER
replied on June 15, 2016

The results file is not a token we can run substitution on.

Your only option would be to write a script to download the e-doc yourself, run the replacement and generate a token you can feed to Read XML.

Be aware that & is allowed in XML as part of escaping of other reserved characters, including ampersand itself, so you would have to do something more than just replace all ampersands with &amp;

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.