[XML Records] Attributes vs Child nodes

[XML Records] Attributes vs Child nodes

  
Forge Component
(24)
Published on 15 Jun by Ricardo Silva
24 votes
Published on 15 Jun by Ricardo Silva
The XML Records extension allows some degree of configuration in the way it exports xml. But some of those changes cannot (currently) be done via the configuration itself. It may require proper naming of attributes or even some post processing of the xml file...

Pedro Miranda wrote:
Hi everyone,

i was looking at the example given and i was started "playing" with the use of configuration to understand it better and i was wondering it it's possible to add attributes with this file and how you do it.

For example, taking the given structure

"<Group testAttribute="TestAttribute">
    <Name>The group name</Name>
    <CreationDateTime>2012-12-27T14:54:12</CreationDateTime>
    <SimpleDate>2012-12-29</SimpleDate>
    <StartTime>15:54:12</StartTime>
    <Decimal>1.12345678</Decimal>
    <HomeAddress>
        <Street>The home street</Street>
        <HouseNumber>15</HouseNumber>
    </HomeAddress>...."

i wanna add the attribute Type="Date" to the CreationDateTime record so it turns like this:

"<Group testAttribute="TestAttribute">
    <Name>The group name</Name>
    <CreationDateTime Type="Date">2012-12-27T14:54:12</CreationDateTime>
    <SimpleDate>2012-12-29</SimpleDate>
    <StartTime>15:54:12</StartTime>
    <Decimal>1.12345678</Decimal>
    <HomeAddress>
        <Street>The home street</Street>
        <HouseNumber>15</HouseNumber>
    </HomeAddress>...."
 
 This cannot be done in the configuration itself.
You'll need to create a new structure in SS, named CreationDateTime, which is a record with 2 attributes:
 - Attr_Type (string)
 - Cont_Content (date)
and add a new attribute to the Group structure that is a Record of the CreationDateTime structure type.

How does it work?
When serializing, the XMLRecords extension will look into the several attributes and process them in sequence.
1. It finds an attribute named CreationDateTime in the Group structure - It will start the new element:
  <CreationDateTime
2. It understands it is a record, and will drill down to its attributes
3. It finds an attribute named Attr_Type. Because the name of the attribute starts with Attr_ (the default - but overridable - prefix that indicates it should export it as an attribute instead of a child element) it exports it as an attribute:
 <CreationDateTime Type="XXXX"
4. It finds an attribute named Cont_Contents. Because the name is the reserved name Cont_ it sets the contents of the parent record, instead of creating a child element named <Contents>
 <CreationDateTime Type="XXXX">2012-12-27T14:54:12</CreationDateTime>
    
Explore the example in detail... It shows pratically everything that is possible to achieve with the configurations. The rest, must be done through naming conventions at the SS structure. This may require you to create some intermediate structures used just to match the exact XML you want to export, and do some data mapping between your source data and the structure that maps the XML you want to export.
Thanks for your explanation and help Gonçalo.

I have another question...imagine i have a structure like the one below.

<Group>
...
      <Location>
           <City>Lisbon</Type>
           <Country>Portugal</Country>
           <DateTime DateType="EarliestDeparture" Type="Date">2012-12-27T17:09:45</DateTime>
      </Location>
      <Location>
           <City>Amsterdam</City>
           <Country>Holland</Country>
           <DateTime DateType="EstimatedArrival" Type="Date">2013-01-27T10:00:00</DateTime>
        </Location>
...
</Group>

The first Location is a Record of the CreationDateTime_Start structure type (just like you told me before but with 2 attributes now) and the second is a Record of the CreationDateTime_Finish structure type, both created equally. My problem here is that if one of this dates is not supplied, i want the element DateTime to not appear in the Location Record, but i'm not being able to do so...if i don't pass a datetime value i get something like:

            <DateTime DateType="EstimatedArrival" Type="Date"> </DateTime>.

Is there a way of telling that if it's a null value to exclude the element, even if it has attributes? And yes, the "Exclude if Null" in the General Options is set to true.

Thanks,
Pedro Miranda
FYI,

and to be more accurate in the definition of my problem, I tried something similar but without attributes with a smiliar structure, as below:

<Group>
...
      <Location>
           <City>Lisbon</Type>
           <Country>Portugal</Country>
           <DateTime DateType="EarliestDeparture" Type="Date">2012-12-27T17:09:45</DateTime>
      </Location>
      <Location>
           <City></City>
           <Country></Country>
           <DateTime></DateTime>
        </Location>
...
</Group>

if i don't give the 2nd location any values, it should exclude this record but it isn't. It only excludes the elements inside the record. So instead of getting:


<Group>
...
      <Location>
           <City>Lisbon</Type>
           <Country>Portugal</Country>
           <DateTime DateType="EarliestDeparture" Type="Date">2012-12-27T17:09:45</DateTime>
      </Location>
...
</Group>

i'm getting:


<Group>
...
      <Location>
           <City>Lisbon</Type>
           <Country>Portugal</Country>
           <DateTime DateType="EarliestDeparture" Type="Date">2012-12-27T17:09:45</DateTime>
      </Location>
      <Location>
        </Location>
...
</Group>
Another problem...in every element that is created with attributes, the value of this element has some "garbage" before and after the value. How can i remove it before creating the xml file?

Below is the example of the same generated file opened with notepad (1) and with a browser:

1- On notepad:
<DateTime DateType="EarliestDeparture" Type="Date">
                2013-01-04T15:01:35
</DateTime>

2-On browser:
<DateTime Type="Date" DateType="EarliestDeparture"> 2013-01-04T15:01:35 </DateTime>

Thanks,
Pedro Miranda
Pedro Miranda wrote:
Thanks for your explanation and help Gonçalo.

I have another question...imagine i have a structure like the one below.

<Group>
...
      <Location>
           <City>Lisbon</Type>
           <Country>Portugal</Country>
           <DateTime DateType="EarliestDeparture" Type="Date">2012-12-27T17:09:45</DateTime>
      </Location>
      <Location>
           <City>Amsterdam</City>
           <Country>Holland</Country>
           <DateTime DateType="EstimatedArrival" Type="Date">2013-01-27T10:00:00</DateTime>
        </Location>
...
</Group>

The first Location is a Record of the CreationDateTime_Start structure type (just like you told me before but with 2 attributes now) and the second is a Record of the CreationDateTime_Finish structure type, both created equally. My problem here is that if one of this dates is not supplied, i want the element DateTime to not appear in the Location Record, but i'm not being able to do so...if i don't pass a datetime value i get something like:

            <DateTime DateType="EstimatedArrival" Type="Date"> </DateTime>.

Is there a way of telling that if it's a null value to exclude the element, even if it has attributes? And yes, the "Exclude if Null" in the General Options is set to true.
 
 
I don't think that is possible with the current extension. You either need to change the extension to do what you need, or do some post processing (e.g. applying some xslt to remove elements with no content.
 
Pedro Miranda wrote:
if i don't give the 2nd location any values, it should exclude this record but it isn't. It only excludes the elements inside the record.
 
 I would say that is a bug... If you get the chance to fix it inside the extension please let me know and I will merge it to a new version. Otherwise, I'll try to check it when I have some time... (Or maybe the other guys in the component team can give it a look??)
As a workaround you can also post process it with a xslt to remove empty elements...
Pedro,

Did you find a solution for removing the 'garbage' in the value of an element?

Kind regards,
Matthias

Pedro Miranda wrote:
Another problem...in every element that is created with attributes, the value of this element has some "garbage" before and after the value. How can i remove it before creating the xml file?

Below is the example of the same generated file opened with notepad (1) and with a browser:

1- On notepad:
<DateTime DateType="EarliestDeparture" Type="Date">
                2013-01-04T15:01:35
</DateTime>

2-On browser:
<DateTime Type="Date" DateType="EarliestDeparture"> 2013-01-04T15:01:35 </DateTime>

Thanks,
Pedro Miranda