Uday Konduru
Uday Konduru

Reputation: 213

XML to HTML transformation

Following is the original XML file that i need to transform.As output I need to show available animations effects per widget per trigger.Like for widet type all and trigger type show all the effect i need like 1,3,6,9.

 <?xml version="1.0" encoding="utf-8"?>
<InputAnimationConfigurationSchema>
<ConfigurationEffects>
<AEffect Id="1" DisplayName="A Effect">
</AEffect>
<BEffect Id="2" DisplayName="B Effect">
</BEffect>
<CEffect Id="3" DisplayName="C Effect">
</CEffect>
<DEffect Id="4" DisplayName="D effect">
</DEffect>
<EEffect Id="5" DisplayName="E effect">
</EEffect>
<FEffect Id="6" DisplayName="F effect">
</FEffect>
<GEffect Id="7" DisplayName="G effect">
</GEffect>
<HEffect Id="8" DisplayName="H effect">
</HEffect>
<IEffect Id="9" DisplayName="I effect">
</IEffect>
<JEffect Id="10" DisplayName="J effect">
</JEffect>
<KEffect Id="11" DisplayName="K effect">
</HEffect>
<LEffect Id="12" DisplayName="L effect">
</KEffect>
</ConfigurationEffects>
<ConfigurationMappings>
<ConfigurationMap>
<Widget Type="All" Include="true" NeedsMandatoryEffectConfiguration="true"/>
<Trigger Type="Show" />
<ConfigurationEffects>
<Effect>1</Effect>
<Effect>2</Effect>
<Effect>3</Effect>
<Effect>9</Effect>
</ConfigurationEffects>
</ConfigurationMap>
<ConfigurationMap>
<Widget Type="All" Include="true" NeedsMandatoryEffectConfiguration="true"/>
<Trigger Type="Hide" />
<ConfigurationEffects>
<Effect>1</Effect>
<Effect>2</Effect>
<Effect>3</Effect>
<Effect>9</Effect>
</ConfigurationEffects>
 </ConfigurationMap>
 <ConfigurationMap>
  <Widget Type="PlaceHolder" Include="false" NeedsMandatoryEffectConfiguration="true"/>
 </ConfigurationMap>
 <ConfigurationMap>
  <Widget Type="PosterItem" Include="false" NeedsMandatoryEffectConfiguration="true"/>
 </ConfigurationMap>
 </ConfigurationMappings>
 </InputAnimationConfigurationSchema>

I was getting the output in the below format:

All        Show     A Effect
--------------------------
All        Show     C Effect
--------------------------
All        Show     F Effect
-------------------------
All        Show     I Effect
----------------------------
All        Hide     A Effect
---------------------------
All        Hide     C Effect
--------------------------
All        Hide     F Effect
----------------------------

But i want to show the output in the below format:

All        Show     A Effect
--------------------------
All        Show     C Effect
--------------------------
All        Show     F Effect
-------------------------
All        Show     I Effect
----------------------------
All        Hide     A Effect
---------------------------
All        Hide     C Effect
--------------------------
All        Hide     F Effect
----------------------------

PlaceHolder ---------------------------- PosterItem


What i am looking is if widget doesn't contain any trigger and effects then also they should display the widget with empty values. Would appreciate if anybody will give me some pointers for performing this task

The xsl code given by stuartLC is the base i took on which i started working but stuck around here for displaying the placeholder and posteritem

Upvotes: 1

Views: 93

Answers (2)

hr_117
hr_117

Reputation: 9627

This is easily possible by changing one line.

Change:

 <td><xsl:value-of select="."/></td>

To:

<td>
    <xsl:value-of select="//ConfigurationEffects/*[@DisplayName][@Id= current()/.]/@DisplayName"/>
</td>

Upvotes: 0

StuartLC
StuartLC

Reputation: 107247

It seems you need to look up the effect from the top ConfigurationEffects. xsl:key is made for this type of job. Note that because it seems the *Effect elements have different names, that I've used * and done the matching on @Id. I've added an $effectId variable to make it clearer, but note that this isn't really necessary.

You should also consider replacing the for-each loops with apply-template

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:key name="effectLookup" match="/InputAnimationConfigurationSchema/ConfigurationEffects/*" use="@Id" />
   <xsl:template match="/">
      <html>
         <body>
            <h2></h2>
            <table border="1">
               <tr bgcolor="#9acd32">
                  <th>Widget</th>
                  <th>Trigger</th>
                  <th>effects</th>
               </tr>
               <xsl:for-each select="/InputAnimationConfigurationSchema/ConfigurationMappings/ConfigurationMap">
                  <xsl:variable name="widgetType">
                     <xsl:value-of select="Widget/@Type"/>
                  </xsl:variable>
                  <xsl:variable name="triggerType">
                     <xsl:value-of select="Trigger/@Type"/>
                  </xsl:variable>
                  <xsl:for-each select="ConfigurationEffects/Effect">
                     <xsl:variable name="effectId">
                        <xsl:value-of select="./text()"/>
                     </xsl:variable>
                     <tr>
                        <td>
                           <xsl:value-of select="$widgetType"/>
                        </td>
                        <td>
                           <xsl:value-of select="$triggerType"/>
                        </td>
                        <td>
                           <xsl:value-of select="key('effectLookup', $effectId)/@DisplayName" />
                        </td>
                     </tr>
                  </xsl:for-each>

               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output

<html>
  <body>
    <h2></h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Widget</th>
        <th>Trigger</th>
        <th>effects</th>
      </tr>
      <tr>
        <td>All</td>
        <td>Show</td>
        <td>A Effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Show</td>
        <td>B Effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Show</td>
        <td>C Effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Show</td>
        <td>I effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Hide</td>
        <td>A Effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Hide</td>
        <td>B Effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Hide</td>
        <td>C Effect</td>
      </tr>
      <tr>
        <td>All</td>
        <td>Hide</td>
        <td>I effect</td>
      </tr>
    </table>
  </body>
</html>

Upvotes: 1

Related Questions