2013-06-19 88 views
1

下面沒有子節點的輸入XML,我有獲取父節點,如果在XSLT

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

請指導我如何來達到同樣的?

+0

@ stuartLC - 這是你的代碼,我複製..需要更多的幫助,以顯示父節點,如果沒有子節點 –

回答

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

+0

@ stuartLC - 因爲這條線 的 小部件中跨越所有的3列,對於所有其他列邊界越來越overlaped..wanna知道如何與他們的邊境保留其他2列,甚至如果沒有數據 –

+0

是的,那是基本的Html。刪除'colspan'屬性,並添加另外2個'td's。注意空'​​'[渲染問題](http://stackoverflow.com/questions/2506342/is-it-good-對放-A-NBSP-內包括空-TD) – StuartLC