1

下面是我拥有的输入xml

<InputAnimationConfigurationSchema>
  <ConfigurationEffects>
  <AEffect Id="1" DisplayName="A Effect">
  </WipeEffect>
  <BEffect Id="2" DisplayName="B Effect">
  </FadeEffect>
  <CEffect Id="3" DisplayName="C Effect">
</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="PIGWidget" Include="false" NeedsMandatoryEffectConfiguration="true"/>
 </ConfigurationMap>
 <ConfigurationMap>
  <Widget Type="PlaceHolder" 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
--------------------------

现在我面临的问题是,如果父节点没有可用的子节点,那么该节点不会被打印,但我还需要显示小部件占位符和 pigwidget

我想要以下格式的输出:

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

对于上面我写的 XSL 是这样的:

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

请指导我如何实现相同的目标?

4

1 回答 1

0

我已将 for 循环重构为模板,并删除了冗余变量。添加了逻辑来区分ConfigurationMaps有无ConfigurationEffect孩子。

<?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:apply-templates 
select="/InputAnimationConfigurationSchema/ConfigurationMappings/ConfigurationMap" />
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="ConfigurationMap">
    <xsl:choose>
      <xsl:when test="not(ConfigurationEffects)">
        <xsl:call-template name="EmptyConfiguration">
          <xsl:with-param name="widgetType" select="Widget/@Type" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="ConfigurationEffects/Effect" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="Effect">
    <tr>
      <td>
        <xsl:value-of select="../../Widget/@Type"/>
      </td>
      <td>
        <xsl:value-of select="../../Trigger/@Type"/>
      </td>
      <td>
        <xsl:value-of select="key('effectLookup', ./text())/@DisplayName" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="EmptyConfiguration">
    <xsl:param name="widgetType" />
    <tr>
      <td colspan="3">
        <xsl:value-of select="$widgetType"/>
      </td>
    </tr>
  </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>
        </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>
        </td>
      </tr>
      <tr>
        <td colspan="3">PIGWidget</td>
      </tr>
      <tr>
        <td colspan="3">PlaceHolder</td>
      </tr>
    </table>
  </body>
</html>

请注意,即使在修复了您的 xml 输入之后,也没有 EffectId 9,因此为空的td.

于 2013-06-19T05:13:51.810 回答