1

I have XML file and my Requirement is to make manual real Carriage Return after 76 chars Into DigestValue node element of that XML file using PHP

By doing that i can validate my XML with per-defined structure of system validator. I am doing that using following line into PHP code.

$digest = wordwrap($digest, 76, "\r\n", true);

However I am getting & #13; which is not proper and I am not able to validate XML file.

I am looking for solution to make manual Carriage Return using PHP but without & #13; into XML string.

So far I have tested following variations.

$digest = wordwrap($digest, 76, "&#xA;", true); // <-- NEW LINE IS WORKING '0A' LF But no Carriage Return HAX Value
$digest = wordwrap($digest, 76, "&#13;&#10;", true);
$digest = wordwrap($digest, 76, "\r", true); // Gives --> IVkHH9IGqaDDHt&#13;iGfODKPJmg==
$digest = wordwrap($digest, 76, "\r\n", true);
$digest = wordwrap($digest, 76, "&#xD;&#xA;", true);
$digest = wordwrap($digest, 76, "&#xD;", true);
$digest = wordwrap($digest, 76, PHP_EOL, true);
$digest = wordwrap($digest, 76, "<![CDATA[\r\n]]>", true);
$digest = wordwrap($digest, 76, "\r&#xA;", true);
$digest = str_replace('&#13;','',$digest);
$digest = str_replace('&\\#13;','',$digest);            
$digest = str_replace('\x13','',$digest);   

Following are some screen shots which might be helpful.

WRONG HEX Viewer

enter image description here

WRONG XML File OUTPUT

enter image description here

CORRECT HEX VALUE

enter image description here

CORRECT XML File OUTPUT

enter image description here

I want suggestion OR PHP solution OR any other way to achieve this. I have tested this script into Windows and Linux, So i think its not OS dependent issue.

Issue Summery : I want to add "Carriage Return" manually to XML string.

Viral
  • 441
  • 9
  • 17
  • Not sure if https://stackoverflow.com/questions/2265966/xml-carriage-return-encoding provides the answer somewhere – Nigel Ren Mar 18 '20 at 14:24
  • I think its not OS dependent issue which adds that #13. I am also looking for some solution for this issue. – Viral Mar 18 '20 at 14:24
  • @NigelRen thanks for you suggestion but i have tested many of those suggestion as mention above. – Viral Mar 18 '20 at 14:25
  • I think there may be a difference between what you might expect and what is an XML standard. As has already been mentioned, new line can be different on different platforms, XML is an independent specification and does not rely on these variations. – Nigel Ren Mar 18 '20 at 14:28

1 Answers1

0

Since an XML parser always converts \r\n into a simple newline character (\n) in your DOM tree, it's normal practice for an XML serializer to assume that if it finds \r in the DOM tree, you put it there for a reason and want it serialized in a way that will survive re-parsing: it can't be output as \r because that won't survive parsing, so it is output as &#13;.

So the question becomes: why are you trying to add a CR character to your XML, when CR characters have no significance in XML? What is the real requirement that makes you want to do this?

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Actually we have system to authenticate this XML file and we are using it from many years with many clients. XML works when we manually build it using JAVA scripts. But we want to automate it now and that's why we are converting that into PHP Web version which helps keep every record online. Basically by adding CR character our system authentication tool will validate it. – Viral Mar 19 '20 at 05:48
  • I get you. Someone did it wrong in the past and you need to stay compatible so you need to find a way to do it wrong now. That can be a challenge. – Michael Kay Mar 19 '20 at 08:35