开发者

How to transform custom XML to Ruby ActiveResource XML format

开发者 https://www.devze.com 2023-03-13 15:35 出处:网络
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.

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&g开发者_开发问答t;
      <count>12</count>
    </facet>
  </facets>
 </facet-group>
</facet-groups>

Please help. Thank you.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消