IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

Condition show element if exist Options · View
hansbm
Posted: Monday, August 21, 2017 11:22:54 AM
Rank: Member

Joined: 8/18/2017
Posts: 11
Location: Netherlands
I'm trying to build an Xpath condition which only shows an xml element when actual filled. Otherwise leave alone, no space or empty value.
It sounds really simple but I can't find an "if exists" condition. There is if-then-else but I'm not sure how to set this up in XPath/StyleVision.
See sps screenshot below.



Line 4, where the cbc:BuildingNumber element shows, starting with the comma I want to hide the cbc:AdditionalStreetName element when there is no value given in XML. See attached files. How would the condition look like? Should I use Xpath for that?

File Attachment(s):
UBLSimple.sps (10kb) downloaded 478 time(s).
2017-0098.xml (14kb) downloaded 678 time(s).
schema.xsd (49kb) downloaded 725 time(s).


hansbm
Posted: Monday, August 21, 2017 11:28:15 AM
Rank: Member

Joined: 8/18/2017
Posts: 11
Location: Netherlands
I've tried the following but that condition won't validate correctly:

if (cbc:AdditionalStreetName) then cbc:AdditionalStreetName else
vlad
Posted: Monday, August 21, 2017 1:16:35 PM
Rank: Advanced Member

Joined: 12/13/2005
Posts: 2,856
Location: Mauritius
But if there is no content it won't appear. What's exactly your problem - the additional empty line?
Also, you have to differentiate between element being present with no value inside, and no element at all. What is your case?
And attach your schema, currently one cannot open your design.

Just by guessing what you want, all you need is to move comma and space within cbc:AdditionalStreetName
hansbm
Posted: Wednesday, August 23, 2017 7:38:39 AM
Rank: Member

Joined: 8/18/2017
Posts: 11
Location: Netherlands
The content needs NOT to be displayed if empty. That's why after the "else" I didn't enter any clause.

So when <cbc:AdditionalStreetName> doesn't have a value, skip it and move to the next (cbc:PostalZone>). The ", " (comma and space) also need not to be displayed.
I'll add the schema to the first post.

The condition
Code:
if (cbc:AdditionalStreetName) then cbc:AdditionalStreetName else

doesn't work in the XPath editor. I can't close the editor (by clicking OK button) because the syntax doesn't comply.
vlad
Posted: Wednesday, August 23, 2017 8:06:47 AM
Rank: Advanced Member

Joined: 12/13/2005
Posts: 2,856
Location: Mauritius
To correct your XPath put () after else

() means empty sequence. But once again, there is no need to put this in an if-else clause, because a missing element doesn't render anyway.
hansbm
Posted: Wednesday, August 23, 2017 11:07:57 AM
Rank: Member

Joined: 8/18/2017
Posts: 11
Location: Netherlands
You are right Sir! I'm sorry, I just realized that empty elements don't display!
I left them alone and made the text before an element conditional. In this example the conition is as follows:
Code:
if (cbc:AdditionalStreetName) then ", " else ()


I made a bunch of other conditions the same way. They all work beautiful.
Thank you.
hansbm
Posted: Tuesday, September 12, 2017 1:28:00 PM
Rank: Member

Joined: 8/18/2017
Posts: 11
Location: Netherlands
I'm now trying to achieve the following.

I need to add telephone/fax/e-mail and other contact details in the mix. They display perfectly but I want to add "tel:"/"fax:"/"email:" before each value conditionally. Not all of these details are mandatory, some fields sometimes just aren't there in the XML file. I've entered the following XPath condition:
Code:
if (cac:Contact/cbc:Telephone) then "tel: " else ()

But this doesn't work. In the example XML given in this thread, the telephone number does exist. The phonenumer itself displays correct on the PDF. What's wrong with the XPath syntax? I have a feeling that the sub ("/") is not recognized.
Users browsing this topic
guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.