Subject:Handling embedded "<" in a source xml file Author:Steve Grise Date:18 May 2007 05:01 PM
I have a source xml document that works within an existing product, but it creates problems for me when I try to use xslt. The xml contains "<" and ">" characters that I'm having a hard time replacing:
<CodedValue xsi:type="es:CodedValue">
<Name>FS - Balsam fir and spruce occur and are each < 10 % of the stand (Balsam Fir dominants); maximum of 20%</Name>
<Code xsi:type="xs:string">FS</Code>
</CodedValue>
Is there a way I can create a template and fix these values in Stylus Studio, or are there any other Javascript etc. solutions for this? I might be able to cheat by replacing " < " with " < " in the xml file but that's obviously a hack.
Subject:Handling embedded Author:Tony Lavinio Date:21 May 2007 08:52 AM Originally Posted: 21 May 2007 08:51 AM
It's not a hack. The < character must be escaped, as in <, if it's
in the middle of text. Otherwise the parser has no clear indication
that it isn't starting an end-tag construct.
That's not just our rule, but is the XML standard. The other parser
is in error, since it is allowing something nonstandard, and the text
of the standard clearly states that conforming XML processors should
act the same in this case.
Subject:Handling embedded Author:Steve Grise Date:22 May 2007 09:03 AM
Thanks, I agree this is non-standard xml. What I meant by "hack" was that I need to rely on a space on either side of the character (ie " < " not " <" or "<" since that would replace a lot of brackets I need.
I notice that Stylus Studio gives me an error message about where the offending character is in the file - is there some way I can get to that error and do the replace functions more systematically?
Subject:Handling embedded Author:Tony Lavinio Date:23 May 2007 10:10 AM Originally Posted: 23 May 2007 10:09 AM
How are you using this file?
I'm assuming that a simple search-and-replace in the editor isn't
sufficient.
If it's in Java, I'd write a wrapper for the InputStream that would
return < whenever it saw a '<' that was preceded and followed by
a space, and use that instead:
InputStream input = new FixupInputStream(input);
For JavaScript, can you grab the whole thing into a string variable
and do a search-and-replace there?
Subject:Handling embedded Author:Steve Grise Date:23 May 2007 10:57 AM
Yes, I'm already doing some Javascript triage on the xml file. I can do a global replace on " < ". I was hoping for something a bit cleaner, but I guess I'll just have to warn people that " < 10" is ok and " <10" is not ok... Seems like replacing "< " and " >" would be catch most of the issues.
I suppose I could also play with some other approaches for catch-and-fix in Javascript rather than global replaces.
Anyway, I think you've answered my basic question - that it's not something I can do easily within Stylus Studio as part of the xslt code I am writing.
Subject:Handling embedded Author:Tony Lavinio Date:23 May 2007 04:56 PM
Of course the real solution, (the one you would get if you posted this
on xml-dev), is that it's not XML, since it's only XML if it follows
all of the rules - and therefore the generating application should be
fixed.
But you already knew that. Sorry you have to suffer for someone else's
bug.