Forums / Developer / How to test an xml text field is empty

How to test an xml text field is empty

Author Message

Paul Forsyth

Saturday 03 May 2003 5:18:09 am

I have some html i wish to display when an xml text field attribute has some data and hide when it does not. There is a graphic header above the text which also hides when the text is empty.

I've put a section around this code to show when the field is not empty but i've found it hard to actually test that the text doesn't contain anything. Because it is an xml text field it by default contains xml. I have had to resort to the following code to allow this test to work. But is there a better way? Perhaps the object should have another attribute to show if it contains anything?

{section name=Hide show=ne($content_version.data_map.my_xmltextfield.data_text,'<?xml version="1.0" encoding="UTF-8"?>
<section />')}

thanks

Paul

Tony Wood

Saturday 03 May 2003 6:04:26 am

On my system the code needs to be

{section name=Hide show=ne($content_version.data_map.my_xmltextfield.data_text,'<?xml version="1.0" encoding=""?>
<section />')}

As the encoding does not show up for me.

Very strange.

Tony Wood : twitter.com/tonywood
Vision with Technology
Experts in eZ Publish consulting & development

Power to the Editor!

Free eZ Training : http://www.VisionWT.com/training
eZ Future Podcast : http://www.VisionWT.com/eZ-Future

Paul Forsyth

Friday 09 May 2003 3:27:46 am

Any comment on this?

Since the xml returned isn't formatted correctly it may be worth filing a bug on that alone...

Paul

Stuart Fenton

Monday 12 May 2003 4:34:37 am

I have tried lots of ways to test if an XML field is empty with no solution.

I have read other article within the EZ site about testing for empty fields using several tests but they don't work for XML fields.

How can I test for an empty field.

I have modified the article object to include multiple columns of information but want to check if the are empty or not and alter the html code presented to change the design into 1,2 or 3 column format.

In order to do this I must test for an empty XML field and change the design accordingly.

How is this achieved?

None of the articles on testing seem to work for XML fields.

Any ideas?

-- Stuart

[email protected]
http://www.grandmore.com

Tony Wood

Thursday 15 May 2003 1:38:38 am

Any ideas eZ?

Tony Wood : twitter.com/tonywood
Vision with Technology
Experts in eZ Publish consulting & development

Power to the Editor!

Free eZ Training : http://www.VisionWT.com/training
eZ Future Podcast : http://www.VisionWT.com/eZ-Future

Bård Farstad

Wednesday 28 May 2003 2:44:29 am

There is actually no good function for this. We should implement an is_empty attribute to check this.

I will start coding right away.

--bård

Documentation: http://ez.no/doc

Bård Farstad

Wednesday 28 May 2003 4:24:32 am

I've just commited a function which does this on ezxmltext datatypes. You can now do:

{section show=$attribute.content.is_empty}
is empty
{section-else}
is not empty
{/section}

Committed revision 2624 in svn.

--bård

Documentation: http://ez.no/doc

Tony Wood

Wednesday 28 May 2003 4:34:42 am

Thanks Bård,

Will this check make it into 3.0 stable as the project this is used for is on latest stable release.

tia

tony

Tony Wood : twitter.com/tonywood
Vision with Technology
Experts in eZ Publish consulting & development

Power to the Editor!

Free eZ Training : http://www.VisionWT.com/training
eZ Future Podcast : http://www.VisionWT.com/eZ-Future

Bård Farstad

Wednesday 28 May 2003 5:06:58 am

Tony, this will not be merged into the 3.0 stable. It will be part of the 3.1 stable which will be relesed in 4 weeks.

--bård

Documentation: http://ez.no/doc

Barney Gumble

Wednesday 21 January 2004 8:17:39 am

I'm using 3.2.4 with the Online Editor, and am still having this problem. It seems that an XML field can never return to the 'empty' state, even after clearing all the text from it. This makes it impossible to use a function such as is_empty.

Looking at the above potential solutions, how can I see the actual XML which the field is left with, i.e. how can I get <?xml version="1.0" encoding="UTF-8"?> or similar to show up? If I knew this then I could check for that string instead of looking for it to be empty.

Paul Forsyth

Wednesday 21 January 2004 8:24:42 am

Do you have a database viewer such as webmin, myphpadmin, or mysql control center? With any of these you will be able to look at the content object attribute and see the xml for yourself.

paul

Marco Zinn

Wednesday 21 January 2004 12:40:08 pm

Hi Barney,
if you think, the problem is the OE, why don't you turn it off temporarly, delete the contents of the field and save the object?
I'm not using OE and tests for "is_empty" were sucessfull.

Marco
http://www.hyperroad-design.com

Barney Gumble

Wednesday 21 January 2004 4:39:37 pm

Paul - Thanks, I was going to try and hook myphpadmin up soon anyway so I might as well go ahead.

Marco - Yes that does in fact work, but I can't rely on the editors to remember to do that each time unfortunately. Fingers crossed for option 1!

Thanks guys as always.

Harry Oosterveen

Thursday 25 March 2004 2:39:37 am

What works well for me, is to add two PHP functions to convert XML text fields into plain trimmed text. Add the following lines in template.ini:

[PHP]
PHPOperatorList[striptags]=strip_tags
PHPOperatorList[wstrim]=trim

(The eZ publish trim function appears not to trim linebreaks)

Now you can test in a template:
{section show=ne($content_version.data_map.my_xmltextfield.data_text|striptags|wstrim)}