如果XSLT中没有子节点,则获取父节点

时间:2013-06-19 04:37:23

标签: java xml xslt

下面是我输入的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>

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

1 个答案:

答案 0 :(得分: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