-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
defmodule XmlNode do | ||
require Record | ||
Record.defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl") | ||
Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl") | ||
|
||
def from_string(xml_string, options \\ [quiet: true]) do | ||
{doc, []} = | ||
xml_string | ||
|> :binary.bin_to_list | ||
|> :xmerl_scan.string(options) | ||
|
||
doc | ||
end | ||
|
||
def all(node, path) do | ||
for child_element <- xpath(node, path) do | ||
child_element | ||
end | ||
end | ||
|
||
def first(node, path), do: node |> xpath(path) |> take_one | ||
defp take_one([head | _]), do: head | ||
defp take_one(_), do: nil | ||
|
||
def node_name(nil), do: nil | ||
def node_name(node), do: elem(node, 1) | ||
|
||
def attr(node, name), do: node |> xpath('./@#{name}') |> extract_attr | ||
defp extract_attr([xmlAttribute(value: value)]), do: List.to_string(value) | ||
defp extract_attr(_), do: nil | ||
|
||
def text(node), do: node |> xpath('./text()') |> extract_text | ||
defp extract_text([xmlText(value: value)]), do: List.to_string(value) | ||
defp extract_text(_x), do: nil | ||
|
||
defp xpath(nil, _), do: [] | ||
defp xpath(node, path) do | ||
:xmerl_xpath.string(to_char_list(path), node) | ||
end | ||
end | ||
|
||
doc = XmlNode.from_string( | ||
""" | ||
<root> | ||
<template> | ||
This test ensures anything inside this template tag is ignored entirely by the parser. | ||
<script>s | ||
food = "bar"; | ||
</script> | ||
<template> | ||
I could be a handlebars template or something, who knows : | ||
</template> | ||
</template> | ||
</root> | ||
""" | ||
) | ||
|
||
IO.inspect(doc) | ||
|
||
Enum.each(XmlNode.all(doc, "//root/template"), fn(node) -> | ||
IO.inspect(node) | ||
IO.puts "#{XmlNode.node_name(node)} id=#{XmlNode.attr(node, "id")} text=#{XmlNode.text(node)}" | ||
end) | ||
|
||
IO.puts( | ||
doc | ||
|> XmlNode.first("//child[@id='2']") | ||
|> XmlNode.text | ||
) | ||
|
||
IO.puts( | ||
doc | ||
|> XmlNode.first("//child[@id='3']") | ||
|> XmlNode.text | ||
) | ||
|
||
IO.puts( | ||
doc | ||
|> XmlNode.first("//root") | ||
|> XmlNode.first("template") | ||
|> XmlNode.text | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<template> | ||
This test ensures anything inside this template tag is ignored entirely by the parser. | ||
|
||
<script> | ||
food = "bar"; | ||
</script> | ||
|
||
<template> | ||
I could be a handlebars template or something, who knows : | ||
</template> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters