Viewed   72 times

I have a previously generated XML like this:

<newsletter>

    <header>
        </magazine>
        </image>
        <strap/>
    </header>

    <intro>
        <date/>
        <text/>
        </edimg>
    </intro>

    <shop>
        <heading/>
        <article/>
        <title/>
        <img/>
        <link/>
        <excerpt/>
    </shop>

    <sidebar>
        <cover/>
        <cover_link/>
        <text/>
        <advert>
        <link/>
        <image/>
        </advert>
    </sidebar>

</newsletter>

I need to be able to insert an element in between the <intro> and the <shop> elements

this:

$section = $dom->documentElement->appendChild($dom->createElement('section'));

will just create the element within <newsletter> .

I assumed this would be fairly simple , but cannot seem to find a solution .

Thanks.

 Answers

3

You might try this; I didn't test it, but the solution comes from using insertBefore instead of appendChild.

$shop = $dom->getElementsByTagName("shop")->item(0);
$section = $dom->documentElement->insertBefore($dom->createElement('section'),$shop);
Friday, November 4, 2022
1

The content property states:

Authors may include newlines in the generated content by writing the "A" escape sequence in one of the strings after the 'content' property. This inserted line break is still subject to the 'white-space' property. See "Strings" and "Characters and case" for more information on the "A" escape sequence.

So you can use:

#headerAgentInfoDetailsPhone:after {
  content:"Office: XXXXX A Mobile: YYYYY ";
  white-space: pre; /* or pre-wrap */
}

http://jsfiddle.net/XkNxs/

When escaping arbitrary strings, however, it's advisable to use 0000a instead of A, because any number or [a-f] character followed by the new line may give unpredictable results:

function addTextToStyle(id, text) {
  return `#${id}::after { content: "${text.replace(/"/g, '\"').replace(/n/g, '\00000a')} }"`;
}
Thursday, August 11, 2022
 
2

Inline elements can't be transformed, and pseudo elements are inline by default, so you must apply display: block or display: inline-block to transform them:

#whatever:after {
  content: "24B6";
  display: inline-block;
  transform: rotate(30deg);
}
<div id="whatever">Some text </div>
Monday, September 26, 2022
2

Ok, let’s try this complete example of use:

function CatRemove($myXML, $id) {
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($myXML);
    $xpath = new DOMXpath($xmlDoc);
    $nodeList = $xpath->query('//category[@id="'.(int)$id.'"]');
    if ($nodeList->length) {
        $node = $nodeList->item(0);
        $node->parentNode->removeChild($node);
    }
    $xmlDoc->save($myXML);
}

// test data
$xml = <<<XML
<?xml version="1.0"?>
<details>
 <person>name</person>
 <data1>some data</data1>
 <data2>some data</data2>
 <data3>some data</data3>
 <category id="0">
  <categoryName>Cat 1</categoryName>
  <categorydata1>some data</categorydata1>
 </category>
 <category id="1">
  <categoryName>Cat 2</categoryName>
  <categorydata1>some data</categorydata1>
  <categorydata2>some data</categorydata2>
  <categorydata3>some data</categorydata3>
  <categorydata4>some data</categorydata4>
 </category>
</details>
XML;
// write test data into file
file_put_contents('untitled.xml', $xml);
// remove category node with the id=1
CatRemove('untitled.xml', 1);
// dump file content
echo '<pre>', htmlspecialchars(file_get_contents('untitled.xml')), '</pre>';
Monday, December 19, 2022
5

You need to import any node to append it to another document:

$departmentArray->item($i)->appendChild( $doc->importNode( $employee, true ) );
Thursday, December 22, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :