Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:将嵌套XML转换为Ruby代码_Ruby_Xml_Xslt_Code Generation - Fatal编程技术网

XSLT:将嵌套XML转换为Ruby代码

XSLT:将嵌套XML转换为Ruby代码,ruby,xml,xslt,code-generation,Ruby,Xml,Xslt,Code Generation,我有几个XML文件需要生成Ruby代码。我的XML结构如下所示: <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

我有几个XML文件需要生成Ruby代码。我的XML结构如下所示:

<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>
我试图解析的其他XML文件也遵循相同的模式。我已经设法让第一部分使用模块声明,但我不知道足够的XSLT来完成其余部分


有人能帮我解决这个问题吗?

我不知道ruby语法,所以我不确定缩进或空格是否重要,但与样式表有关

<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>

这太棒了。谢谢如果我想使item元素的处理变得通用(即,我有一个文件带有首字母缩略词\u item,另一个文件带有资产\u item),我可以像上面那样对模式采取类似的方法吗?在您发布的示例中,有一个
首字母缩略词列表
元素,但是没有
首字母缩略词\u item
,所以我不确定
资产\u item
会出现在哪里。现在还不清楚你想要哪种输出。如果想要的输出结构没有任何变化,那么只需为模板提供匹配模式的枚举就足够了,例如
。模式有助于对节点进行多次处理,即第一次使用第一种模式以某种方式输出,第二次使用第二种方式输出
<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>
<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>
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