Skip to main content
Question

How to replace an xml element with an ampersand in it


jakethepainter
Contributor
Forum|alt.badge.img+6

I'm using the xmlupdater, but have encountered a problem when there is an ampersand in the path that I'm looking up. The updater doesn't find it. In the xml I'm reading in, all of the ampersands have been changed to & but if I send that as part of the string into the path part of the updater I never find a match. That's true if I use a value type of string or of xml/xquery.

4 replies

todd_davis
Influencer
Forum|alt.badge.img+22
  • Influencer
  • August 25, 2016

Are you saying that the ampersand is actually in the element tag (e.g. <name&address;>)? If so, that is not valid in XML.


takashi
Influencer
  • August 26, 2016

Hi @jakethepainter, XML element/attribute names cannot contain ampersands, but their values can contain ampersands as entity reference representation "&amp;". e.g.

<?xml version="1.0"?>
<root>
  <data v="a&amp;b">c&amp;d</data>
  <data v="e&amp;f">g&amp;h</data>
</root>

Then, this XPath finds elements whose attribute value is equal to "a&amp;b".
//data[@v="a&amp;b"]

Is you case similar to this?

jakethepainter
Contributor
Forum|alt.badge.img+6

To todd_davis - I didn't use quotes in my description so the formatter of the discussion board changed my "&" to a regular one.  Sorry for the misleading question.   

Here's an example of what I've got. If I send a feature into the xmlupdater with an attribute called OLD_WMS_CODE where the value is "ASPHC&R2 G \ MH \ CU" and have an attribute called DESCRIPTION where the value is "UPDATED" and set the xmlpath to "//CU[WMS_CODE=fme:get-attribute("OLD_WMS_CODE")]/DESCRIPTION", I don't see the DESCRIPTION changing in the output xml.  In the values that don't have ampersands in them, though, they update just fine.

<CU ProgID="mmDesktop.MMCompatibleUnit.1"> 
	<DESCRIPTION>ASPHALT CUT&REPLACE; 2 FT WIDE</DESCRIPTION>
	<WMS_CODE>ASPHC&R2 G \ MH \ CU</WMS_CODE> 
 </CU>

takashi
Influencer
  • August 30, 2016

Hi @jakethepainter, I was able to update the document with the following setting.

Source XML fragment (ampersands should be written with entity references)

<CU ProgID="mmDesktop.MMCompatibleUnit.1"> 
	<DESCRIPTION>ASPHALT CUT&amp;REPLACE 2 FT WIDE</DESCRIPTION>
	<WMS_CODE>ASPHC&amp;R2 G \ MH \ CU</WMS_CODE> 
</CU>
Value of 'OLD_WMS_CODE' attribute (the ampersand should NOT be written with entity reference)
ASPHC&R2 G \ MH \ CU
XMLUpdater parameters

0684Q00000ArKQrQAN.png

Resulting XML document
<?xml version="1.0" encoding="UTF-8"?>
<CU ProgID="mmDesktop.MMCompatibleUnit.1"> 
	<DESCRIPTION>NEW&amp;DESCRIPTION</DESCRIPTION>
	<WMS_CODE>ASPHC&amp;R2 G \ MH \ CU</WMS_CODE> 
</CU>

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings