Asked  7 Months ago    Answers:  5   Viewed   39 times

I have well-formed xml documents into string variables. I want to use preg_replace to add a defined attribute to every xml tags.

For example replace:

<tag1>
<tag2> some text </tag2>
</tag1>

by:

<tag1 attr="myAttr">
<tag2 attr="myAttr"> some text </tag2>
</tag1>

So I basically need the regex expression to find any start tags and add my attribute, but I'm a complete regex noob.

 Answers

93

Don't use regular expressions for working on xml. Xml is not a regular language. Use the xml extensions of php instead:

$xml = new SimpleXml(file_get_contents($xmlFile));
function process_recursive($xmlNode) {
    $xmlNode->addAttribute('attr', 'myAttr');
    foreach ($xmlNode->children() as $childNode) {
        process_recursive($childNode);
    }
}
process_recursive($xml);
echo $xml->asXML();

All answers containing regular expressions will break this valid xml, for example:

<?xml version="1.0" encoding='UTF-8'?>
<html>
    <head>
        <!-- <meta> ... </meta> -->
        <script>//<![CDATA[
            function load() {document.write('<tt>Test</tt>');}
        //]]></script>
        <title><![CDATA[Fancy <<SiteName>> [with Breadcrumbs] > in > title]]></title>
    </head>
    <body onload="load()">
        <input
            type="submit"
            value="multiline
                   button
                   text"
        />
    </body>
</html>
Wednesday, March 31, 2021
 
christina
answered 7 Months ago
28

I dont want xml writer to encode the multilingual characters , how this is possible ?

Actually as you write XML you already encode. What you mean is that you don't want to use numeric entities for these two characters which is possible but not always.

To not use numeric entities, you need to match the encoding of the document with the encoding of your string. From the output you provided I can only guess a bit, those two characters probably stand for:

  1. Unicode Han Character 'the Chinese people, Chinese language' (U+6F22)
  2. Unicode Han Character 'letter, character, word' (U+5B57)

Which could mean (I do not speak any Chinese so far) something like Chinese Word.

XMLWriter in PHP will always put characters into a numeric entity (like &#x6F22; and &#x5B57; in your example) whenever the encoding of the document is not able to represent that character within the document.

If you are able to match both encodings XMLWriter will automatically not use the numeric entities.

I give a more simple example. Let's take the US-ASCII encoding and the German umlaut Ä from Äpfel (Unicode Character 'LATIN CAPITAL LETTER A WITH DIAERESIS' (U+00C4)) as an attribute value:

<?php
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', 'US-ASCII');
$xmlWriter->startElement('root');
$xmlWriter->writeAttribute('value', 'Äpfel');
$xmlWriter->endDocument();
echo $xmlWriter->flush();

This code written down in an UTF-8 encoded PHP file will output when executed:

<?xml version="1.0" encoding="US-ASCII"?>
<root value="&#196;pfel"/>

&#196; is the numeric entity for the unicode character U+00C4 and if you look closely, C4 is the hexadecimal representation of decimal 196 which also shows that the numeric XML entity always represents the Unicode character number.

So the XML output uses the US-ASCII encoding which is not able to represent the Ä from the UTF-8 encoded string in the PHP code and therefore properly encodes it with it's numeric entity to preserve the character information.

Now changing the encoding from:

$xmlWriter->startDocument('1.0', 'US-ASCII');

to the UTF-8 encoding of the PHP string:

$xmlWriter->startDocument('1.0', 'UTF-8');

does change this output:

<?xml version="1.0" encoding="UTF-8"?>
<root value="Äpfel"/>

This would equally work with your example however, one important information in your question is missing: In which encoding is the string from that record?

If it is UTF-8 already, then like I outlined in the example above, it would work already:

<?php
$recordUTf8 = "... contents="Just <span style="color:red">testing</span>:"
             ."xE6xBCxA2xE5xADx97"";
$encoding   = 'UTF-8';
$encoding   = 'US-ASCII';

$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', $encoding);
$xmlWriter->startElement('record');
$xmlWriter->writeAttribute('value', $recordUTf8);
$xmlWriter->endDocument();
echo $xmlWriter->flush();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<record value="... contents=&quot;Just &lt;span style=&quot;color:red&quot;&gt;
               testing &lt;/span&gt;:?? &quot;"/>

As this output show, no numeric entities are used here, however, the string is clearly UTF-8 encoded (in a binary safe manner here in case you use a different encoding for the PHP file if you copy it over).

So just to summarize at this point: The XML encoding need to match the encoding of the string to represent all characters not in numeric entities (apart from the ones used to encode XML itself like <, >, ', " and &).

These are pretty much XML basics. If the document has an encoding the character data can not be represented in but as XML supports Unicode, the fallback are numeric entities. You are trying to prevent this fallback by aligning the document encoding with the string encoding.

Here is my advice for PHP & XMLWriter specifically:

  1. Obtain or re-encode the record from the database to UTF-8.
  2. Only pass UTF-8 strings into XMLWriter methods.
  3. Set the XML documents encoding to UTF-8.

I give these suggestions because UTF-8 is the default encoding of XML and UTF-8 support is quite well in PHP. Also XMLWriter expects Unicode strings to be UTF-8 encoded, there is no setting or option that allows you to change that, so the input already needs to be UTF-8 encoded.

However independent to the input string, you can naturally tell XMLWriter to use a different output encoding. For example any other Chinese or Unicode Encoding might be suitable for you and it is possible for XMLWriter output as long as your PHP configuration supports that specific output encoding (check the iconv library you have).

When you start the document with XMLWriter, the second parameter specifies the encoding:

$xmlWriter->startDocument('1.0', $encoding);

You can put in any encoding from the set of the encodings XML supports in the corresponding XML-Declaration:

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Latin-1 example -->

The full specs of the XML encoding value can be found here: http://www.w3.org/TR/REC-xml/#NT-EncName ::

In an encoding declaration, the values " UTF-8 ", " UTF-16 ", " ISO-10646-UCS-2 ", and " ISO-10646-UCS-4 " should be used for the various encodings and transformations of Unicode / ISO/IEC 10646, the values " ISO-8859-1 ", " ISO-8859-2 ", ... " ISO-8859- n " (where n is the part number) should be used for the parts of ISO 8859, and the values " ISO-2022-JP ", " Shift_JIS ", and " EUC-JP " should be used for the various encoded forms of JIS X-0208-1997. It is recommended that character encodings registered (as charsets) with the Internet Assigned Numbers Authority [IANA-CHARSETS], other than those just listed, be referred to using their registered names; other encodings should use names starting with an "x-" prefix. XML processors should match character encoding names in a case-insensitive way and should either interpret an IANA-registered name as the encoding registered at IANA for that name or treat it as unknown (processors are, of course, not required to support all IANA-registered encodings).

Where-as [IANA-CHARSETS] is:

(Internet Assigned Numbers Authority) Official Names for Character Sets, ed. Keld Simonsen et al. (See http://www.iana.org/assignments/character-sets.)

These specs are perhaps a little bit verbose. In the context of your question, all you need to do is to find out the encoding of your record-string. I btw. can't say I was not able to reproduce your exact output, I always get decimal entities, not hexa-decimal ones. You might be able to provide more information with a hex-dump of the string.

Wednesday, March 31, 2021
 
mopsyd
answered 7 Months ago
76

I don't know about the (somewhat weird) syntax suggested by JJ (and according to your reposting of the question, it doesn't seem to work), but you should be able to shape your request xml anyway you want by constructing it more or less 'manually' using the SoapVar class with the XSD_ANYXML encoding.

See this user comment to the SoapVar Constructor documentation for an example of how to do this.

If the XML you need to construct gets more complex, consider using SimpleXML or DOMDocument to assemble it instead of writing it directly.

(It still seems odd to me that there should be no simpler way to do this, but so far I have not found one.)


Edit: Just found another example using the XSD_ANYXML encoding in this question asking for a simpler way to do it. Unfortunately no one came up with a simpler way so far :/

Wednesday, March 31, 2021
 
Owen
answered 7 Months ago
46

You should add the following flags to your regex:

  • m to enable multiline strings
  • u to enable UTF8 strings (if necessary)
Wednesday, March 31, 2021
 
radmen
answered 7 Months ago
19

You can add attributes using attr like so:

$('#someid').attr('name', 'value');

However, for DOM properties like checked, disabled and readonly, the proper way to do this (as of JQuery 1.6) is to use prop.

$('#someid').prop('disabled', true);
Wednesday, June 9, 2021
 
Whakkee
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :