XML到HTML的转换

时间:2013-06-18 07:05:24

标签: java xml xslt

以下是我需要转换的原始XML文件。作为输出,我需要显示每个触发器每个触发器的可用动画效果。类似于widet类型all和触发器类型显示我需要的所有效果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>

我的输出格式如下:

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

但我希望以下面的格式显示输出:

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

占位符     ----------------------------    PosterItem


我看的是如果小部件不包含任何触发器和效果,那么他们也应该显示具有空值的小部件。如果有人会给我一些执行此任务的指示,我将不胜感激

stuartLC给出的xsl代码是我开始工作的基础但是在这里停留以显示占位符和posteritem

2 个答案:

答案 0 :(得分:1)

您似乎需要从顶部ConfigurationEffects查找效果。 xsl:key是为这类工作而制作的。 请注意,因为*Effect元素似乎有不同的名称,我使用了*并在@Id上完成了匹配。我添加了$effectId变量以使其更清晰,但请注意,这不是必需的。

您还应考虑将for-each循环替换为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>

<强>输出

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

答案 1 :(得分:0)

通过更改一行可以轻松实现。

更改:

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

要:

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