Lax <> wrote:
> I'm trying to search and replace the value of a tag in an xml file.
No you're not.
You are trying to search and replace the value of an element in an xml file.
See the XML FAQ:
http://xml.silmaril.ie/authors/makeup/
> Sample xml file:
> -------------------------
>
><project xmlns="xml:header">
>
> <version>1.0.0</version>
>
> <SomeTag>
> <version>invalid version</version>
> </SomeTag>
>
>
> <SomeAnotherTagNested1>
> <SomeAnotherTagNested2>
> <SomeAnotherTagNested3>
> <version>invalid version</version>
> </SomeAnotherTagNested3>
> </SomeAnotherTagNested2>
> </SomeAnotherTagNested1>
>
> <version>stand-alone, but not valid either</version>
>
></project>
>
> -------------------------
>
> I only want the version tag when they're not enclosed in any other
> tags.
It is not legal in XML for a tag to enclose any other tag.
(tags start with a '<' and end with a '>')
You must have meant "element" where you said "tag".
In that case, there ARE NO version elements that are not enclosed
in any other elements!
> I want to replace the 1.0.0 (an example value) with 2.0.0
That element is enclosed in the project element.
> on an stand-
> alone "version"'s first occurence.
You want to replace the 1.0.0 with 2.0.0 on the first version element
that is a child of the document element (the project element in this case).
(in which case you have a poor example input, as a solution that
operates on the first <version> anywhere in the file will work
for that input...
)
> The above script works, and a "diff bak <xml-file>" gives me the
> expected result when the stand-alone <version> is all on one line, I
> cant get this working when its extended over multiple-lines.
Extended over multiple lines in what manner? Like this:
<version
>1.0.0</version>
or like
<version>
1.0.0</version>
or like
<version>
1.0.0
</version>
??
Those all are legal XML, but none of them are equivalent, they each
have different content.
> Could anyone give me some pointers, please?
If I could unambiguously figure out what you really want I probably could...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"