2012-01-13 72 views
0

我创建了一个xsl文件,它将输出两个XML:s。从外部XML文件输出

其中一个xml应该只被收割一次。因为它只有一个结构和它的工作,所以我不会发布代码, 但在另一个我想输出整个树结构。但它的唯一打印第一个。而不是整棵树。

这病想出此刻

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 

<html> 
<body> 


<xsl:for-each select="$doc1//quiz//question"> 
<xsl:value-of select="$doc1//author" /> 
<br /> 
<xsl:value-of select="$doc1//questionText" /> 
<br /> 
<xsl:text>Ger </xsl:text><xsl:value-of select="$doc1//points" /><xsl:text> Poäng </xsl:text> 
</xsl:for-each> 

</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

感谢

编辑: 威尔广告我的XML文件

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="q2.xsl"?> 
<quiz> 
<question> 
<author>Författare 1</author> 
<questionText>Fråga 1</questionText> 
<correct>Svar 1</correct> 
<incorrect>fel 1</incorrect> 
<incorrect>fel 1-2</incorrect> 
<points>1</points> 
</question> 

<question> 
<author>Författare 2</author> 
<questionText>Fråga 2</questionText> 
<correct>Svar 2</correct> 
<incorrect>fel 2</incorrect> 
<incorrect>fel 2-3</incorrect> 
<points>2</points> 
</question> 

<question> 
<author>Författare 3</author> 
<questionText>Fråga 3</questionText> 
<correct>Svar 3</correct> 
<incorrect>fel 3</incorrect> 
<incorrect>fel 3-4</incorrect> 
<points>3</points> 
</question> 

<question> 
<author>Författare 4</author> 
<questionText>Fråga 4</questionText> 
<correct>Svar 4</correct> 
<incorrect>fel 4</incorrect> 
<incorrect>fel 4-5</incorrect> 
<points>4</points> 
</question> 

<question> 
<author>Författare 5</author>+ 
<questionText>Fråga 5</questionText> 
<correct>Svar 5</correct> 
<incorrect>fel 5</incorrect> 
<incorrect>fel 5-6</incorrect> 
<points>5</points> 
</question> 

</quiz> 
+0

你可以添加你的xml,那么它可能更容易提供一个解决方案。 – Treemonkey 2012-01-13 11:36:03

回答

2

你的问题是你引用回到原始当您输入for-each语句

而不是实际上下文节点时,请输入

这应该使用应用模板工作

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 

<html> 
<body> 


<xsl:for-each select="$doc1/quiz/question"> 
<xsl:value-of select="author" /> 
<br /> 
<xsl:value-of select="questionText" /> 
<br /> 
<xsl:text>Ger </xsl:text><xsl:value-of select="points" /><xsl:text> Poäng </xsl:text> 
</xsl:for-each> 

</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

额外的例子!

这对阅读效果好得多。一旦事情变得复杂,并且您可以轻松注意到nodeset的上下文!

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/">  
     <html> 
      <body> 
       <xsl:apply-templates select="$doc1/quiz/question"/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="question"> 
     <xsl:value-of select="author" /> 
     <br /> 
     <xsl:value-of select="questionText" /> 
     <br /> 
     <xsl:text>Ger </xsl:text> 
     <xsl:value-of select="points" /> 
     <xsl:text> Poäng </xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 
+0

O我的,谢谢你一直在为此奋斗了近一个小时:) – kakka47 2012-01-13 11:51:18

+0

欢迎你,而不是for-每个你可以使用apply-templates match =“blah”然后创建一个新的模板来匹配我将添加示例 – Treemonkey 2012-01-13 11:52:38

+0

其实是刚开始试验:) 感谢:) – kakka47 2012-01-13 11:55:14