Question

How to replace an xml element with an ampersand in it

  • 25 August 2016
  • 4 replies
  • 38 views

Badge +5

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

Userlevel 1
Badge +12

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

Userlevel 2
Badge +17

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?
Badge +5

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>
Userlevel 2
Badge +17

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>

Reply