Skip to content

Commit 13556df

Browse files
authored
Merge pull request #348 from BuildingSync/feat/translation
Feat/translation
2 parents 05c8989 + 9088d45 commit 13556df

21 files changed

+154
-31475
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ gems
99
.python-version
1010
BuildingSync.xpr
1111
schema_documentation.csv
12-
geojson.xsd
12+
geojson.xsd
13+
spec/output

spec/data/v2_for_translation.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<auc:BuildingSync xmlns:auc="http://buildingsync.net/schemas/bedes-auc/2019"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://buildingsync.net/schemas/bedes-auc/2019 https://raw.githubusercontent.com/BuildingSync/schema/v2.3.0/BuildingSync.xsd" version="2.3.0">
4+
<auc:Facilities>
5+
<auc:Facility ID="Facility-1">
6+
<auc:Measures>
7+
<auc:Measure ID="Measure-1">
8+
<auc:TechnologyCategories>
9+
<auc:TechnologyCategory>
10+
<auc:OtherHVAC>
11+
<auc:MeasureName>Repair leaks / seal ducts</auc:MeasureName>
12+
<auc:MeasureName>Convert CV system to VAV system</auc:MeasureName>
13+
</auc:OtherHVAC>
14+
</auc:TechnologyCategory>
15+
</auc:TechnologyCategories>
16+
</auc:Measure>
17+
</auc:Measures>
18+
</auc:Facility>
19+
</auc:Facilities>
20+
</auc:BuildingSync>

spec/validation_spec.rb

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@
4444
# and point schemaLocation to it instead
4545
schema_doc = Nokogiri::XML(File.read('BuildingSync.xsd'))
4646

47-
GEOJSON_XSD_PATH = 'geojson.xsd'
48-
GEOJSON_IMPORT_XPATH = 'xs:schema/xs:import[@namespace = "http://www.gbxml.org/schema"]'
49-
if !File.file?(GEOJSON_XSD_PATH) then
50-
imported_schema_locations = schema_doc.xpath(GEOJSON_IMPORT_XPATH).collect { |nokogiri_xml_node|
47+
GBXML_XSD_PATH = 'gbxml.xsd'
48+
GBXML_IMPORT_PATH = 'xs:schema/xs:import[@namespace = "http://www.gbxml.org/schema"]'
49+
if !File.file?(GBXML_XSD_PATH) then
50+
imported_schema_locations = schema_doc.xpath(GBXML_IMPORT_PATH).collect { |nokogiri_xml_node|
5151
nokogiri_xml_node.attribute("schemaLocation").value
5252
}
5353
expect(imported_schema_locations.length).to eq 1
5454

55-
open(GEOJSON_XSD_PATH, 'wb') do |file|
55+
open(GBXML_XSD_PATH, 'wb') do |file|
5656
file << open(imported_schema_locations[0]).read
5757
end
5858
end
5959

60-
schema_doc.xpath(GEOJSON_IMPORT_XPATH).collect { |nokogiri_xml_node|
61-
nokogiri_xml_node.attribute("schemaLocation").value = GEOJSON_XSD_PATH
60+
schema_doc.xpath(GBXML_IMPORT_PATH).collect { |nokogiri_xml_node|
61+
nokogiri_xml_node.attribute("schemaLocation").value = GBXML_XSD_PATH
6262
}
6363

6464
@xsd = Nokogiri::XML::Schema.from_document(schema_doc)
@@ -389,3 +389,52 @@
389389
expect(dupe_names).to be_empty
390390
end
391391
end
392+
393+
RSpec.describe 'Version translation from v2 to v3' do
394+
before :all do
395+
# TODO: DRY this up --- duplicate code from example validators spec above
396+
397+
# Nokogiri doesn't seem to support XSDs which import other schemas with URLs
398+
# for the schemaLocation. To allow testing, we download the imported schema
399+
# and point schemaLocation to it instead
400+
schema_doc = Nokogiri::XML(File.read('BuildingSync.xsd'))
401+
402+
GBXML_XSD_PATH = 'gbxml.xsd'
403+
GBXML_IMPORT_PATH = 'xs:schema/xs:import[@namespace = "http://www.gbxml.org/schema"]'
404+
if !File.file?(GBXML_XSD_PATH) then
405+
imported_schema_locations = schema_doc.xpath(GBXML_IMPORT_PATH).collect { |nokogiri_xml_node|
406+
nokogiri_xml_node.attribute("schemaLocation").value
407+
}
408+
expect(imported_schema_locations.length).to eq 1
409+
410+
open(GBXML_XSD_PATH, 'wb') do |file|
411+
file << open(imported_schema_locations[0]).read
412+
end
413+
end
414+
415+
schema_doc.xpath(GBXML_IMPORT_PATH).collect { |nokogiri_xml_node|
416+
nokogiri_xml_node.attribute("schemaLocation").value = GBXML_XSD_PATH
417+
}
418+
419+
@xsd = Nokogiri::XML::Schema.from_document(schema_doc)
420+
421+
# clean up output dir
422+
FileUtils.rm_rf('spec/output')
423+
FileUtils.mkdir_p('spec/output')
424+
end
425+
426+
it 'should result in valid v3 file' do
427+
doc = Nokogiri::XML(File.read('spec/data/v2_for_translation.xml'))
428+
xslt_doc = Nokogiri::XML(File.read('translation/v2_to_v3.xsl'))
429+
# we must remove xsl:message from the stylesheet b/c nokogiri does not handle
430+
# them properly -- it considers it a failure.
431+
# See: https://github.com/sparklemotion/nokogiri/issues/1217
432+
xslt_doc.search('//xsl:message').remove
433+
xslt = Nokogiri::XSLT::Stylesheet.parse_stylesheet_doc(xslt_doc)
434+
435+
transformed_doc = xslt.transform(doc)
436+
File.write('spec/output/v2_to_v3.xml', transformed_doc)
437+
438+
expect(@xsd.validate(transformed_doc)).to be_empty
439+
end
440+
end

translation/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Translation
2+
To assist with "updating" users BuildingSync documents, this directory provides XSLTs for translating one version into another.
3+
Note that the translation is not guaranteed to be perfect as there might not always be a one to one mapping.
4+
We will list any known limitations of the translators in the [limitations](#limitations) section below
5+
6+
## Usage
7+
If using Linux or a Mac, make sure xsltproc is installed by running
8+
```bash
9+
xsltproc
10+
```
11+
Then transform your file(s) with
12+
```bash
13+
xsltproc [-o <output>] <stylesheet> <file> [<file> ...]
14+
```
15+
Where `-o <output>` can optionally be included to save the result, `<stylesheet>` is the path to the desired `.xsl` file, and `<file>` is the path to the file you'd like to transform.
16+
17+
If on Windows, either find another XSLT processor or use an online processor.
18+
19+
## Limitations
20+
### v2 to v3
21+
There are no known limitations.

translation/v2_to_v3.xsl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
5+
XML Stylesheet Language Transformation (XSLT) to transform BuildingSync from v2 to v3
6+
7+
Version: 0.1.0
8+
9+
-->
10+
11+
<xsl:stylesheet version="1.0"
12+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
13+
xmlns:auc="http://buildingsync.net/schemas/bedes-auc/2019"
14+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
15+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
exclude-result-prefixes="xs">
17+
18+
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
19+
20+
<xsl:template match="/">
21+
<xsl:comment>This BuildingSync v3.0 document was generated from a BuildingSync v2.X document via an XML Stylesheet Language Transformation (XSLT).</xsl:comment>
22+
<xsl:text>&#10;</xsl:text>
23+
<xsl:copy>
24+
<xsl:apply-templates/>
25+
</xsl:copy>
26+
</xsl:template>
27+
28+
<xsl:template match="@* | node()">
29+
<xsl:copy>
30+
<xsl:apply-templates select="@* | node()"/>
31+
</xsl:copy>
32+
</xsl:template>
33+
34+
<xsl:template match="@xsi:schemaLocation">
35+
<xsl:attribute name="xsi:schemaLocation">http://buildingsync.net/schemas/bedes-auc/2019 https://raw.githubusercontent.com/BuildingSync/schema/v3.0.0/BuildingSync.xsd</xsl:attribute>
36+
</xsl:template>
37+
38+
<xsl:template match="@auc:version">
39+
<xsl:attribute name="auc:version">3.0.0</xsl:attribute>
40+
</xsl:template>
41+
42+
<!-- Map "Repair leaks / seal ducts" to "Repair leaks in ducts" -->
43+
<xsl:template match="auc:MeasureName[. = 'Repair leaks / seal ducts']">
44+
<xsl:copy>Repair leaks in ducts</xsl:copy>
45+
</xsl:template>
46+
47+
<!-- Limit auc:MeasureName to exactly one by commenting out all other auc:MeasureNames -->
48+
<xsl:template match="auc:MeasureName[not(position() = 1)]">
49+
<xsl:message>WARNING: commenting out auc:MeasureName <xsl:value-of select="current()"/></xsl:message>
50+
<xsl:comment>
51+
<xsl:apply-templates select="@* | node()"/>
52+
</xsl:comment>
53+
</xsl:template>
54+
55+
</xsl:stylesheet>

translator/css/agate.min.css

Lines changed: 0 additions & 17 deletions
This file was deleted.

translator/css/bootstrap.min.css

Lines changed: 0 additions & 5 deletions
This file was deleted.
-19.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)