2013-08-20 47 views
0

我有一个XML文件,它具有许多类似命名的节点,但某些节点中的属性是唯一的。我只想输出到HTML页面中的属于某个属性值的节点。xslt 1.0 - 尝试获取特定值的节点

这里是XML:

<document> 
    <component> 
    <section> 
     <templateId value="temp_1" /> 
     <entry> 
     <act> 
      <code displayName="temp_1:code_1" /> 
     </act> 
     </entry> 
     <entry> 
     <act> 
      <code displayName="temp_1:code_2" /> 
     </act> 
     </entry> 
     <entry> 
     <act> 
      <code displayName="temp_1:code_3" /> 
     </act> 
     </entry> 
    </section> 
    <section> 
     <templateId value="temp_2" /> 
     <entry> 
     <act> 
      <code displayName="temp_2:code_1" /> 
     </act> 
     </entry> 
     <entry> 
     <act> 
      <code displayName="temp_2:code_2" /> 
     </act> 
     </entry> 
    </section> 
    </component> 
</document> 

从这个具体的例子,我想只能从有temp_2的templateId值的部分DisplayName值。这是我正在使用的XSL代码,但它获取了所有内容,而不仅仅是我想要的部分。我知道第一个“何时”正在工作,因为正确的标题(跨度标记之间)正常显示。这只是为了每个参赛作品。

<xsl:tempalte match="/"> 
<xsl:choose> 
    <xsl:when test="//templateId/@value='temp_2'"> 
    <div style="margin-bottom: 5px; padding: 5px; border-bottom: 1px solid #000000;"> 
     <span style="font-weight: bold;">Template 2: </span> 
     <br /> 
     <xsl:choose> 
     <xsl:when test="count(//section/entry) != 0"> 
      <xsl:for-each select="//section/entry"> 
      <xsl:choose> 
       <xsl:when test="position() = 1"> 
       <xsl:value-of select="act/code/@displayName" /> 
       </xsl:when> 
       <xsl:otherwise> 
       <br/> 
       <xsl:value-of select="act/code/@displayName" /> 
       </xsl:otherwise> 
      </xsl:choose> 
      </xsl:for-each> 
     </xsl:when> 
     <xsl:otherwise> 
      No codes to display 
     </xsl:otherwise> 
     </xsl:choose> 
    </div> 
    </xsl:when> 
</xsl:choose> 
</xsl:template> 

它应该显示像这样:

temp_2:code_1 
<br>temp_2:code_2 

任何帮助将不胜感激。

+0

这将有助于查看您期望收到的输出。 – ABach

+0

@ABach - 增加什么输出应该是 – jjasper0729

回答

0

根据你的描述,这听起来像你只想要for-each中的temp_2值。

既然如此你可以更新你选择以下:

<xsl:for-each select="//section[templateId/@value = 'temp_2']/entry"> 

此说抓住任何entrysection具有templateId带有等于“temp_2”的value的属性。

+0

我是否也会把这个在when语句正上方for-each我检查计数以查看是否有条目?我需要检查是否有任何条目,如果不是,请在其他位置显示文本。 – jjasper0729

+0

是的,你可以。否则,您的测试时间是查看您所有的部分/条目而不是temp_2中的特定部分。 –

+0

现在就与这个一起...努力解释torazaburo的其他答案,以便于长期使用。谢谢 – jjasper0729

1

我想你想完全重新研究XSLT及其哲学。不要将它编程为BASIC。至少在你的情况下,基本模式是XSLT程序是处理匹配元素的模板集合。用ifchoose乱丢代码,而不是用适当的匹配条件编写模板。代替BASIC的FOR I=1 TO 10,使用<xsl:apply-templates/>来“遍历”子代。这里的基本思想是:

<xsl:template match="/"> 
    <html> 
     <xsl:apply-templates/> 
    </html> 
</xsl:template> 

<xsl:template match="templateId"/> <!-- skip templateID elements by default --> 

<xsl:template match="templateId[@value='temp_2']"> 
    <div style="margin-bottom: 5px; padding: 5px; border-bottom: 1px solid #000000;">   
     <span style="font-weight: bold;">Template 2: </span> 
     <xsl:apply-templates/> 
    </div> 
</xsl:template> 

<xsl:template match="code"> 
    <xsl:value-of select="@displayName"/> 
    <xsl:if test="position() != 1"><br/></xsl:if> 
</xsl:template> 

<xsl:template match="section[count(entry)=0]"> 
    No codes to display 
</xsl:template> 

为什么为act元素没有模板?那么,默认情况下,XSLT将为您提供一个模板,它可以执行<xsl:apply-templates/>

+0

我只是从vb.net背景进入XSLT,所以我的想法是在.net进程中。如果我想输出模板1,但如果与模板2中的模板布局不同,这将如何工作? – jjasper0729

+0

所以我应该有每种不同类型的输出为不同的节点(不只是在这个例子中的这些,因为它是一个很大的xml文件的片段)一个模板,满足需求,并通过XML文档节点集下工作?您拥有的,它经历了所有的“从...调用?它可以是特定的,以便不同的部分可以有自己的模板?如果这样更容易阅读,那么这对我更有帮助。 – jjasper0729

+0

添加另一个模板' 2013-08-20 18:09:31