GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes text in XML tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "text in XML tag" Watch "text in XML tag" New topic
Author

text in XML tag

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

Hello,

I need something like this

"<element1> some text here
<element2></element2>
</element1>"
(please ignore the double quotes)

The problem is that element1 should be unbounded so I don't specify the type and make element2 its child.
But as element1 is of no particular type, so text can't be added to it.
If I make element1 as string/integer type then I can't add element2 as element1 is of a particular type.

Please advise.

Thanks.


Thanks,
Pramod
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
XML has no notion of types, so there must be something else in play that you didn't mention. Are you using a library or some other piece of software that restricts what can be added to the XML? Is there maybe an XML Schema that applies? TellTheDetails.


Ping & DNS - my free Android networking tools app
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

Hi Ulf,

Thanks for the quick response.

Below is the schema I'm using



This is the xml file for above schema :


This is the error message I get while trying to validate the xml file:-
Text '"

Send comments and suggestions to <url protocol="mailto">barchal@pineapplesoft.com</url>.

"' is not allowed for element <sc:section>. The element declaration's content type is 'element-only.


The xml schema is validated.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
If you want to transport HTML in a validated XML document you need to use CDATA sections to hide the HTML, or escape the special XML characters by using &lt; &gt; etc.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

hI Ulf,

I already had CDATA section but got the same error again.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Post the XML that uses CDATA.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

The CDATA use is as follows :-

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

The <![CDATA[ ...code..]]> gets removed after posting.

The start code is this :-

<sc:section>
<![CDATA[<p>Send comments and suggestions to <url protocol="mailto">barchal@pineapplesoft.com</url>.</p>
</sc:section>


and the end code is :-
<p>This means that a document can be rendered differently depending on the media or the audience. For example, a "managerial" style sheet may present a summary view of a document that highlights key elements but a "clerical" style sheet may display more detailed minformation.</p>]]>


The error message is :-
Text '<p>Send comments and suggestions to <url protocol="mailto">barchal@pineapplesoft.com</url>.</p>' is not allowed for element <sc:section>. The element declaration's content type is 'element-only'.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

I tried out this one too, but the same error :-

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
As long as <p> is in there you'll likely have problems, since that makes it mixed content. Try escaping it using &lt; and &gt;. As it is, the document isn't even well-formed, since the <p> tags have no closing </p> tags.

I'm not an XML Schema expert, though, so there may be better approaches to this.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

Hi Ulf,

I tried out &l t; (ignore the space between l & t) as well as < but got the same error again and again.
I removed "< p>" as you said, but no effect on the error message.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
I thought it went without saying that you need to treat all other HTML tags (like <url>) the same way.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

I can't remove the url tag as it is necessary for me to have it.
If I remove it then there is no meaning left for CDATA as well.

Also, even though the p tag doesn't need an end tag, the document is well formed after I test it and the book I'm using for reference does show "</" p>.

Should I keep just one CDATA tag for the entire document or have it for each section tag?
If CDATA ignores the markup tags then what is the use of writing &l t; instead of "<" in CDATA[ ] as both serve the same purpose.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
I can't remove the url tag as it is necessary for me to have it.

I didn't say "remove it", I said "escape it". You'd need to un-escape it before you can use the payload as HTML, just like with the <p> tag.

Also, even though the p tag doesn't need an end tag

It does if you use it in an XML document - otherwise the document is not well-formed. Alternatively, you can use "<p/>".

Should I keep just one CDATA tag for the entire document or have it for each section tag?

CDATA is for the payload, not for the actual XML tags. So you'd need them for each section tag.

If CDATA ignores the markup tags then what is the use of writing &l t; instead of "<" in CDATA[ ] as both serve the same purpose.

Yes, only one of these is necessary, not both.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 321

I didn't say "remove it", I said "escape it". You'd need to un-escape it before you can use the payload as HTML, just like with the
tag.

Yes, only one of these is necessary, not both.


Even If I try with either &l t; or < for the url tag, I get the same error.



Note:-I have used the escape characters instead of<, > in the url tag above.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
I can get the XML document to validate if I
  • escape "p" and "url" elements
  • add mixed="true" to the complexType declaration
  • add minOccurs="0" to the "element title" declaration

  • pramod talekar
    Ranch Hand

    Joined: Apr 26, 2010
    Posts: 321

    Hi Ulf,

    I have set the title element's minOccur =0, that's fine and also used escape characters for tags.
    Let me see on Monday what I've used for mixed.

    Thanks for the advice.

    Cheers.

    Gute Nacht.
    pramod talekar
    Ranch Hand

    Joined: Apr 26, 2010
    Posts: 321

    Hi Ulf,

    It's working fine.

    Thanks for the help.

    Could you please tell me what mixed="true" does.

    Cheers.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42061
        
      64
    See the page on mixed content in this XML Schema tutorial: http://www.w3schools.com/schema/default.asp
    pramod talekar
    Ranch Hand

    Joined: Apr 26, 2010
    Posts: 321

    Awesome!
    Thanks a lot.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: text in XML tag