best practice for xml repeating elements

2019-07-19 08:28发布

问题:

Is it acceptable to repeat xml elements as shown in this example? Notice in the first example how the 'period_data' element is just repeated directly inside the 'usage_history' element without first being wrapped inside something like a 'periods' parent element.

Initially it seems reduant to me to include the 'periods' parent element as the only thing inside the 'usage_history' parent are 'period_data' elements anyway.

Thank you.

<usage_history num_periods="2">
    <period_data billing_year="2013" billing_period="2">
        content...
    </period_data>
    <period_data billing_year="2013" billing_period="1">
        content...
    </period_data>
</usage_history>

as opposed to this...

<usage_history>
    <periods num_periods="2">
        <period_data billing_year="2013" billing_period="2">
            content...
        </period_data>
        <period_data billing_year="2013" billing_period="1">
            content...
        </period_data>
    </periods>
</usage_history>

回答1:

Yes. That is fine. There is no need for an extra enclosing element.

I would also say that num_periods="2" is wrong, as the period_data elements should account for themselves.



标签: xml repeat