Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gatsby-transformer-xml #1465

Merged
merged 5 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 156 additions & 1 deletion packages/gatsby-transformer-xml/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,158 @@
# gatsby-transformer-xml

Stub README
Parses XML files. It supports also attributes

## Install

`npm install --save gatsby-transformer-xml`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
`gatsby-transformer-xml`,
]
```

## Parsing algorithm

The algorithm for arrays is to convert each item in the array into
a node.

So if your project has a `bookss.xml` with

```xml
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
```
Then the following 2 nodes will be created

```javascript
{
"root":{
"name":"catalog",
"attributes":{

},
"children":[
{
"name":"book",
"attributes":{
"id":"bk101"
},
"children":[
{
"name":"author",
"attributes":{

},
"children":[

],
"content":"Gambardella, Matthew"
},
{
"name":"title",
"attributes":{

},
"children":[

],
"content":"XML Developer's Guide"
}
],
"content":""
},
{
"name":"book",
"attributes":{
"id":"bk102"
},
"children":[
{
"name":"author",
"attributes":{

},
"children":[

],
"content":"Ralls, Kim"
},
{
"name":"title",
"attributes":{

},
"children":[

],
"content":"Midnight Rain"
}
],
"content":""
}
],
"content":""
}
}
```

## How to query

You'd be able to query your books like:

```graphql
{
allBooks {
edges {
node {
content
}
}
}
}
```

Which would return:

```javascript
{
allBooks: {
edges: [
{
node: {
content: 'Gambardella, Matthew'
}
},
{
node: {
content: 'XML Developer's Guide
}
}
]
}
}
```
5 changes: 5 additions & 0 deletions packages/gatsby-transformer-xml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1"
},
"dependencies": {
"bluebird": "^3.5.0",
"lodash": "^4.17.4",
"xml-parser": "^1.2.1"
}
}
Loading