user374760
user374760

Reputation: 409

How to transform custom XML to Ruby ActiveResource XML format

I am new to XSLT, please help. We have an XML which we want to convert it into Ruby ActiveResource format XML. Please help me with the XSLT code. Thank you.

XML INPUT

<PARM>
<PC>0</PC>
<PMT NM="THEME" DN="THEME" IR="0" T="0">
    <PV V="fast" L="" H="" C="4"/>
</PMT>
<PMT NM="INGREDIENTS" DN="INGREDIENTS" IR="0" T="0">
    <PV V="chicken" L="" H="" C="5"/>
    <PV V="tomato" L="" H="" C="12"/>
</PMT>
</PARM>

REQUIRED XML OUTPUT

<facet-groups type="array">
 <facet-group>
  <name>THEME</name>
  <facets type="array">
    <facet>
      <name>fast</name>
      <count>4</count>
    </facet>
  </facets>
 </facet-group>
 <facet-group>
  <name>INGREDIENTS</name>
  <facets type="array">
    <facet>
      <name>chicken</name>
      <count>5</count>
    </facet>
    <facet>
      <name>tomato</name>
      <count>12</count>
    </facet>
  </facets>
 </facet-group>
</facet-groups>

Please help. Thank you.

Upvotes: 1

Views: 242

Answers (2)

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

This is the @LarsH in other words:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="PARM">
        <facet-groups type="array">
            <xsl:apply-templates select="PMT"/>
        </facet-groups>
    </xsl:template>

    <xsl:template match="PMT">
        <facet-group>
            <name><xsl:value-of select="@NM"/></name>
            <facets type="array">
                <xsl:apply-templates select="PV"/>
            </facets>
        </facet-group>
    </xsl:template>

    <xsl:template match="PV">
        <facet>
            <name><xsl:value-of select="@V"/></name>
            <count><xsl:value-of select="@C"/></count>
        </facet>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 2

LarsH
LarsH

Reputation: 28004

As @Keoki said, you should show more of what you've done, before it would be appropriate to give you a whole solution. But to get started, you would create

  • a template matching "PARM", that outputs a facet-groups element and within that, applies templates to children

  • a template matching "PMT", that outputs a facet-group element, with children <name> and <facets>, and within the latter, applies templates to children

  • a template matching "PV", that outputs a facet element, with children <name> and <count>

Hopefully that will give you a good start.

Upvotes: 2

Related Questions