I am creating an XML document using django, and looking at the XSD schema, a lot of the tags may or may not be needed.
Like so:
<GenericCustomerPaymentDetails>
<PayPalID>{{purchase.customer.ppid}}</PayPalID>
<BankAccountNumber>{{purchase.customer.ban}}</BankAccountNumber>
<SortCode>{{purchase.customer.sc}}</SortCode>
<CreditCardNumber>{{purchase.customer.ccn}}</CreditCardNumber>
<BitCoinAddress>{{purchase.customer.bitcoin}}</BitCoinAddress>
</GenericCustomerPaymentDetails>
Now, I know how to individually specify that a tag may or may not exist (wrap in an if/endif
tags), but it's going to triple the size of the document, and double the amount of maintenance to do this:
<GenericCustomerPaymentDetails>
{% if purchase.customer.ppid %}
<PayPalID>{{purchase.customer.ppid}}</PayPalID>
{% endif %}
{% if purchase.customer.ban%}
<BankAccountNumber>{{purchase.customer.ban}}</BankAccountNumber>
{% endif %}
{% if purchase.customer.sc %}
<SortCode>{{purchase.customer.sc}}</SortCode>
{% endif %}
{% if purchase.customer.ccn %}
<CreditCardNumber>{{purchase.customer.ccn}}</CreditCardNumber>
{% endif %}
{% if purchase.customer.bitcoin %}
<BitCoinAddress>{{purchase.customer.bitcoin}}</BitCoinAddress>
{% endif %}
</GenericCustomerPaymentDetails>
If there a more elegant way to do this? Is there a way to bulk apply an if exists to the value and the tag? (bonus points if the solution can accommodate stags [self-closing tags])
The only way I can think of doing this is make these payment methods into a list of objects on the json like so
purchase.customer:[
{tag_name:"PayPalID",tag_content:"pay.me.monies@geemail.com"},
{tag_name:"BitCointAddress",tag_content:"http://blockexplorer.com/address/1PC9aZC4hNX2rmmrt7uHTfYAS3hRbph4UN"},
]
And then loop over them. But that will require extra data manipulation to get into this format, and I'd rather not have to go through that effort (if looks like this is the way to go, if you already have data that way, though.).