2013-05-30 38 views
1

我有以下XML,我想提取所有title_en属性。XSLT匹配父项和子项

<quiz> 
    <question title_de="Seit wann wird Appenzeller® Käse hergestellt?" 
       title_fr="Depuis quand le fromage d’Appenzell est-il fabriqué?" 
       title_en="For how long has Appenzeller cheese been made?" > 
     <answer title_de="Über 7 Jahre!" title_fr="Depuis plus de 7 ans !" title_en="For over 7 years !"></answer> 
     <answer title_de="Über 70 Jahre!" title_fr="Depuis plus de 70 ans !" title_en="For over 70 years !"></answer> 
     <answer title_de="Über 700 Jahre!" title_fr="Depluis plus de 700 ans !" title_en="For over 700 years !"></answer> 
    </question> 
</quiz>   

这是我的XSLT:

<xsl:template match="answer"> 
    <tr> 
     <td><xsl:value-of select="@title_en"/></td> 
    </tr>   
    </xsl:template> 

    <xsl:template match="question"> 
    <tr> 
     <td><xsl:value-of select="@title_en"/></td> 
    </tr>   
    </xsl:template> 

我可以从问题或答案中获得属性,但从来没有。我已经尝试了所有类型的匹配语句的

+0

您没有在'question'的模板内调用'xsl:apply-templates',所以'answer'元素永远不会被处理。 – 2013-05-30 13:53:31

回答

2

你似乎只有已包括你的XSLT的一部分,也许这是你在找什么:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/quiz"> 
     <xsl:apply-templates select="question"/> 
    </xsl:template> 
    <xsl:template match="answer"> 
     <tr> 
      <td> 
       <xsl:value-of select="@title_en"/> 
      </td> 
     </tr> 
    </xsl:template> 
    <xsl:template match="question"> 
     <tr> 
      <td> 
       <xsl:value-of select="@title_en"/> 
       <xsl:apply-templates select="answer"/> 
      </td> 
     </tr> 
    </xsl:template> 
</xsl:stylesheet> 

我认为你只是缺少<xsl:apply-templates select="answer"/>从问题模板。您需要修改HTML,因为现在在问题TD中输出TR标签,如下所示:

<tr> 
    <td>For how long has Appenzeller cheese been made?<tr> 
      <td>For over 7 years !</td> 
     </tr> 
     <tr> 
      <td>For over 70 years !</td> 
     </tr> 
     <tr> 
      <td>For over 700 years !</td> 
     </tr> 
    </td> 
</tr> 
+0

对,我有点短: - ( 但第二个做到了。 – Remy

0

请注意,请尝试如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:template match="quiz"> 
    <xsl:choose> 
     <xsl:when test="question[@title_en]"> 
     <xsl:apply-templates/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:apply-templates select="question/answer"/> 
     </xsl:otherwise> 
    </xsl:choose> 

    </xsl:template> 

    <xsl:template match="answer"> 
    <tr> 
     <td> 
     <xsl:value-of select="@title_en"/> 
     </td> 
    </tr> 
    </xsl:template> 

    <xsl:template match="question"> 
    <tr> 
     <td> 
     <xsl:value-of select="@title_en"/> 
     </td> 
    </tr> 
    </xsl:template> 
</xsl:stylesheet>