dnewcome / jath

Jath is a simple template language for parsing xml using json markup.
MIT License
68 stars 15 forks source link

how can we write a jath template for the following xml #10

Closed sijuthankachan closed 9 years ago

sijuthankachan commented 12 years ago

how can we write a template for the following xml

My XML style

  <label id='ep' added="2003-06-10">
    <name>Ezra Pound</name>
    <address>
      <street>45 Usura Place</street>
      <city>Hailey</city>
      <province>ID</province>
    </address>
  </label>

  <label id='ep2' added="2003-06-20">
    <name>Siju</name>
    <address>
      <street>3 Prufrock Lane</street>
      <city>Stamford</city>
      <province>ID</province>
    </address>
    <address>
      <street>2nd address</street>
      <city>2nd city</city>
      <province>2nd id</province>
    </address>
    <address>
      <street>3rd address</street>
      <city>3rd city</city>
      <province>3rd id</province>
    </address>
  </label>
...
</labels>```

Into Javascript like this

view source
print?
```[
  {
    id: "ep",
    added: "2003-06-10",
    address: {
      street: "45 Usura Place",
      city: "Hailey"
    }
  },
  {
    id:"ep2",
    added: "2003-06-20",
    address:[
    {
        street: "3 Prufrock Lane",
        city: "Stamford"
        },
    {
        street: "2nd address",
        city: "2nd city"
        },
    {
        street: "3rd address",
        city: "3rd city"
        }

  },
  ...
]```
dnewcome commented 12 years ago

The following nested template should do what you want:

var template = [               
    '//label', { 'id': '@id', 'added': '@added,
        'address': [ 'address', { 'street': 'street', 'city': 'city' } ]                                  
} ];       

The address field selector just needs to be another jath array template.

sijuthankachan commented 12 years ago

the above code didnt help me.. please

this is my original XMl Structure.

  <data>
    <list> 
        <item>
          <map>
            <prdid>1</prdid>
            <prdname>Product 1</prdname>
            <prddec>Product 1 Dec</prddec>
            <prddate>2012-06-19 05:57:28.0</prddate>
            <type>true</type>
          </map>
        </item> 
        <item>
          <map>
            <prdid>2</prdid>
            <prdname>Product 2</prdname>
            <prddate>2012-06-14 11:38:30.0</prddate>
            <type>true</type>
            <prddec>
              <list> 
                <item>
                    <map>
                      <text>Product 2 Dec Title 1</text>
                      <type>Product 2 Dec Type 1</type>
                    </map>
                </item> 
                <item>
                    <map>
                      <text>Product 2 Dec Title 2</text>
                      <type>Product 2 Dec Type 2</type>
                    </map>
                </item> 
                <item>
                    <map>
                      <text>Product 2 Dec Title 2</text>
                      <type>Product 2 Dec Type 2</type>
                    </map>
                </item>
              </list>
            </prddec>
          </map>
        </item> 
        <item>
          <map>
            <prdid>3</prdid>
            <prdname>Product 3</prdname>
            <prddec>Product 3 Dec</prddec>
            <prddate>2012-06-14 11:38:28.0</prddate>
            <type>true</type>
          </map>
        </item> 
    </list>
  </data>
</map>```

and i need the jath template for this

```[
  {
    prdid: "1",
    prdname: "Product 1",
    prddec: "Product 1 Dec"
    prddate: "2012-06-19 05:57:28.0",
    type: "true"
  },
  {
    prdid: "2",
    prdname: "Product 1",
    prddec: [
                {
                text:'Product 2 Dec Title 1'
                type:'Product 2 Dec Type 1'
                },
                {
                text:'Product 2 Dec Title 2'
                type:'Product 2 Dec Type 2'
                },
                {
                text:'Product 2 Dec Title 3'
                type:'Product 2 Dec Type 3'
                },                              
            ]
    prddate: "2012-06-14 11:38:30.0",
    type: "true"
  },
  {
    prdid: "3",
    prdname: "Product 3",
    prddec: "Product 3 Dec"
    prddate: "2012-06-14 11:38:28.0",
    type: "true"
  }
]```

is it possible with Jath?
dnewcome commented 12 years ago

Your

<prddec>

Field has 2 different formats. There is no alternation in Jath currently, so I think you'd have to allow for 2 output fields, for example, "prddec" and "prddec_full".

Here is a sample snippet:

{ 'prddec': 'prddec', 'prddec_full': [ 'list/item', { 'text': 'map/text', 'type': 'map/type' } ] }

I haven't tested it, I'm just trying to show the basic idea.

I'm going to think about supporting some kind of conditional template or alternation, but I'm not sure what that's going to look like yet. Let me know if you have any syntax suggestions.