" in XML-Source and IF Statement in Word Template leads to Mailmerge-Error

Aspose.words Java 21.10

example.zip (1.1 MB)

Hi :wave:,
I’m trying to use conditional mailmerge fields (if-statements) with &quot ; (") in my xml source.
This leads to the following error:

Elementname: D
Attribut: Name="""INDEX:0
java.lang.IllegalStateException
        at com.aspose.words.zzZ99.zznj(Unknown Source)
        at com.aspose.words.zzYqX.zzWgd(Unknown Source)
        at com.aspose.words.zzYqX.getText(Unknown Source)
        at com.aspose.words.zzYqX.zzcp(Unknown Source)
        at com.aspose.words.zzYqX.zzZOL(Unknown Source)
        at com.aspose.words.zzYoi.zzWH7(Unknown Source)
        at com.aspose.words.zzYoi.zzZeH(Unknown Source)
        at com.aspose.words.zzTk.zzz4(Unknown Source)
        at com.aspose.words.zzTk.zzYsF(Unknown Source)
        at com.aspose.words.zzTk.zzW3B(Unknown Source)
        at com.aspose.words.zzTk.zzWhE(Unknown Source)
        at com.aspose.words.zzTk.zzVOs(Unknown Source)
        at com.aspose.words.zzTk.zzZa5(Unknown Source)
        at com.aspose.words.zzTk.zzWB7(Unknown Source)
        at com.aspose.words.zzTk.zzYBC(Unknown Source)
        at com.aspose.words.zzZo.zzWPL(Unknown Source)
        at com.aspose.words.zzZo.zzY7o(Unknown Source)
        at com.aspose.words.zzZo.zzWPL(Unknown Source)
        at com.aspose.words.MailMerge.zzWPL(Unknown Source)
        at com.aspose.words.MailMerge.executeWithRegions(Unknown Source)
        at aspose_master.aspose_app.App.handleRegions(App.java:75)

When Mailmerge function is executed it throws the error, if I use &apos ; instead it throws no error, but I need to work with the xml source file that already uses &quot ; in attribute values.

I have attached a short example mvn project with both xml cases and a junit test, just run mvn test -e
to see the error on the " xml source.

Any advice to avoid this error?

Best regards

@bennetpe Thank you for reporting the problem to us. For a sake of correction it has been logged as WORDSNET-23893. We will keep you informed and let you know once it is resolved.
As a workaround, you can modify your data source like the following (add \ before quot;):

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <A>
    <C Nummer="" />
    <D Name="\&quot;"/>
  </A>
</root>

Hi Alexey,

thank you for your quick reply.
I’ve tried your workaround advice and the error does not appear anymore, but it seems not correct escaping the quot, because it is rendered after mailmerge like &quot; in the pdf document.

I’ve changed the xml source like above directly and also tried to iterate over DOM Nodes and replaced attribute nodes values containing the &quot ; - char sequence.

Both result in the same output, any advice what I’m doing wrong here?

  1. tried with xml:

     <?xml version="1.0" encoding="UTF-8"?>
     <root>
      <A>
        <C Nummer="" />
        <D Name="\&quot;"/>
      </A>
    </root>
    
  2. DOM-Replacement
    2.1 XML:

     <?xml version="1.0" encoding="UTF-8"?>
      <root>
      <A>
        <C Nummer="" />
        <D Name="&quot;"/>
      </A>
    </root>
    

    2.2 Replacement Function Snippet:

     if (element.getAttributes().item(x).getNodeValue().contains("\"")) {
     	String escapedQuot = 
            String.valueOf(element.getAttributes().item(x).getNodeValue())
     					.replaceAll("\"", "\\&quot;");
     		element.setAttribute(
                                   element.getAttributes().item(x).getNodeName(),
                                   escapedQuot);
     	 }
    

Best regards

quot.pdf (31.1 KB)
example.zip (1.0 MB)

@bennetpe Thank you for additional information.

  1. This XML works properly on my side if do not perform additional replacement:
<root>
	<A>
		<C Nummer="" />
		<D Name="\&quot;"/>
	</A>
</root>

  1. in case of replacement, you can use this:
String escapedQuot = String.valueOf(element.getAttributes().item(x).getNodeValue())
	.replaceAll("\"", "\\\\\"");
1 Like

Thank you very much for the hint, this works now as expected if only one quote is in xml.
But usally I have words with opening and closing &quot ; in xml
like:

<?xml version="1.0" encoding="UTF-8"?>
   <root>
	<A>
		<C Nummer="" />
		<D Name="&quot;Some Text&quot;"/>
	</A>
</root>

with:

 // Workaround for &quot; bug in xml attributes
 if (element.getAttributes().item(x).getNodeValue().contains("\"")) {
      String input = element.getAttributes().item(x).getNodeValue();
	  String escapedQuot = input.replaceAll("\"", "\\\\\"");
      System.out.println("Escaped String "+ escapedQuot);
      element.setAttribute(
                  element.getAttributes().item(x).getNodeName(),
                  escapedQuot);
      }

I get the escaped value: \" Some Text \" wich seems correct, however in pdf after mailmerge the first \" is correct merged into " whereby the second enclosing (and all following) \" merged into \".

So the recived value in pdf looks like: "Some Text\" instead of "Some Text".

Any advice to avoid on this behaviour?

Best regards
quot.pdf (31.2 KB)

@bennetpe Yes, this quite strange issue. Also there is strange behavior if &quot; is not the first character:

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<A>
		<C Nummer="" />
		<D Name="test\&quot;test\&quot;"/>
	</A>
</root>

This does not thrown an exception, but both are rendered as escaped \". I have added this information into the created defect. Unfortunately, I did not manage to find a workaround for this. Our development team will analyze the issue and we provide you more information.

that’s right, but thanks for your support anyway :slight_smile:

1 Like

The issues you have found earlier (filed as WORDSNET-23893) have been fixed in this Aspose.Words for Java 22.7 update also available on Maven.

1 Like