Reputation: 5821
I have several XML files that I need to generate Ruby code for. The XML structure I have is as follows:
<acronym_list>
xmlns="http://www.example.com/xsds"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/xsds
http://www.example.com/xsds/acronyms.xsd">
<item>
<metadata>
<release>...</release>
<id>...</id>
<checkdigit>...<checkdigit>
<status>...</status>
<date_added>...</date_added>
<date_modified>...</date_modified>
<language>...</language>
<license_url>...</license_url>
</metadata>
<info>
<name>...</name>
</info>
</item>
</acronym_list>
In this case, we're talking about acronyms. The item
elements (and their children) are repeated for each acronym I have in my list. I also have several files with a similar structure with the info
element having more children.
The Ruby code I'm trying to get out would look like:
Module acronym_list
def self.included(other)
include SAXMachine
SAXMachine.configure(other) do |c|
c.element :metadata, :class => metadata
c.element :info, :class => info
end
end
class metadata
include SAXMachine
c.element :release
c.element :id
c.element :checkdigit
c.element :status
c.element :date_added
c.element :date_modified
c.element :language
c.element :license_url
end
class info
include SAXMachine
c.element :name
end
end
The same pattern follows for the other XML files that I'm trying to parse. I've managed to get the first part working with the module declaration, but I don't know enough XSLT to get the rest.
Would somebody be able to help me out with this?
Upvotes: 0
Views: 227
Reputation: 167696
I don't know the ruby syntax so I am not sure whether indentation or whitespace matters but with the stylesheet
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:acs="http://www.example.com/xsds"
exclude-result-prefixes="acs">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="acs:acronym_list">
Module <xsl:value-of select="local-name()"/>
def self.included(other)
include SAXMachine
SAXMachine.configure(other) do |c|
<xsl:apply-templates select="acs:item/acs:*" mode="config"/>
end
end
<xsl:apply-templates select="acs:item/acs:*" mode="class"/>
end
</xsl:template>
<xsl:template match="acs:item/acs:*" mode="config">
c.element :<xsl:value-of select="local-name()"/>, :class => <xsl:value-of select="local-name()"/>
</xsl:template>
<xsl:template match="acs:item/acs:*" mode="class">
class <xsl:value-of select="local-name()"/>
include SAXMachine
<xsl:apply-templates mode="class"/>
end
</xsl:template>
<xsl:template match="acs:item/acs:*/acs:*" mode="class">
c.element :<xsl:value-of select="local-name()"/>
</xsl:template>
</xsl:stylesheet>
Saxon 6.5.5 tansforms the input
<acronym_list
xmlns="http://www.example.com/xsds"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/xsds
http://www.example.com/xsds/acronyms.xsd">
<item>
<metadata>
<release>...</release>
<id>...</id>
<checkdigit>...</checkdigit>
<status>...</status>
<date_added>...</date_added>
<date_modified>...</date_modified>
<language>...</language>
<license_url>...</license_url>
</metadata>
<info>
<name>...</name>
</info>
</item>
</acronym_list>
into the output
Module acronym_list
def self.included(other)
include SAXMachine
SAXMachine.configure(other) do |c|
c.element :metadata, :class => metadata
c.element :info, :class => info
end
end
class metadata
include SAXMachine
c.element :release
c.element :id
c.element :checkdigit
c.element :status
c.element :date_added
c.element :date_modified
c.element :language
c.element :license_url
end
class info
include SAXMachine
c.element :name
end
end
Upvotes: 1