Adding Attributes to all XML Nodes
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 5
- Joined: Mon Jan 21, 2008 10:59 am
Adding Attributes to all XML Nodes
Post by cesarcesar »
Howdy,
I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page.
This is the current code i am using to edit specific nodes. Im trying to use DOM, but it doesn't have to be. I just need it to work.
This is the XML file im working with. **NOTE** the missing *id* attributes of the 2nd *child01* node.
I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page.
This is the current code i am using to edit specific nodes. Im trying to use DOM, but it doesn't have to be. I just need it to work.
Code: Select all
<?php
$content = $_POST['content'];
$id = $_POST['id'];
$node = $_POST['node'];
$explode = explode("_",$node);
$dom=new DOMDocument();
$dom->load('sample.xml');
$dom->formatOutput = true;
//echo $dom->saveXML(); // show before file
$allnodes = $dom->getElementsByTagName($explode[0]);
foreach ($allnodes as $nodes) {
if ($nodes->nodeName==$explode[0] and $nodes->getAttribute('id')==$id) { //
$nodes->setAttribute($explode[1],$content);
}
}
$dom->save('sample.xml');
?>
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<parent option01="other info" option02="other info" id="1">
<child01 option01="child01 option01" option02="child01 option02" id="2">
<child02 option01="child02 option01" option02="child02 option02" id="3">
<text option01="text option01" option02="text option02" id="4">
<![CDATA[<b>Ma quande lingues coalesce</b>]]>
</text>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="5">image 1</image>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="6">image 2</image>
</child02>
<child02 option01="child02 option01" option02="child02 option02" id="7">
<text option01="text option01" option02="text option02" id="8">
<![CDATA[Lorem ipsum dolor sit amet.]]>
</text>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="9">image 1</image>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="10">image 2</image>
</child02>
</child01>
<child01 option01="child01 option01" option02="child01 option02">
<child02 option01="child02 option01" option02="child02 option02">
<text option01="text option01" option02="text option02">
<![CDATA[Epsum factorial non deposit quid pro quo hic escorol.]]>
</text>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
</child02>
<child02 option01="child02 option01" option02="child02 option02">
<text option01="text option01" option02="text option02">
<![CDATA[Li Europan lingues es membres del sam familie.]]>
</text>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
<image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
</child02>
</child01>
</parent>
-
- Posts: 501
- Joined: Mon Feb 03, 2003 10:56 am
-
- Posts: 5
- Joined: Mon Jan 21, 2008 10:59 am
Post by cesarcesar »
Yes PHP has nice XML handling. What i have posted works... i now want to add the numbering of the nodes. Any ideas on this?
-
- Posts: 7
- Joined: Sat Mar 08, 2008 12:03 am
Re: Adding Attributes to all XML Nodes
Post by KermitTensmeyer »
You know about an Identity Transformation right?
then for each element, have it test for an existing ID and strip it out.
now add to each target element a new attribute called id with the specified value.
for example:
hope this helps Kermit Tensmeyer
then for each element, have it test for an existing ID and strip it out.
now add to each target element a new attribute called id with the specified value.
for example:
Code: Select all
<xsl:template match="parent|child01|child02|child03">
<xsl:copy select=".">
<xsl:attribute name="id"><xsl:value-of select="...assign new value.."/>
</xsl:attribute>
</xsl:copy>
</xsl:template>
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service