我已经回顾了可能已有您的答案部分的问题,但解决方案无效。
我是XML样式表的新手,我有一个XSL,在我的每个循环中,当我试图获取标记中的所有文本时,数据不断重复。它具有计数(父标记内的值的数量)是准确的,但只打印相同的文本。
有什么想法吗?
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/programming/languages/language">
<h3>=======Programming Language <xsl:value-of select="./@id" />=======</h3>
<p>Language: <xsl:value-of select="name" /></p>
Intended Use:
<xsl:variable name="intendedUseID" select="purpose/intendedUse/@id" />
<xsl:for-each select="/programming/purpose/intendedUse[@id=$intendedUseID]" >
<xsl:value-of select="/programming/purpose/intendedUse" />
</xsl:for-each>
<br />
<xsl:variable name="featureID" select="features/feature/@id" />
<xsl:for-each select="/programming/features/feature[@id=$featureID]">
<xsl:value-of select="/programming/features/feature" /><br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML
<programming>
<languages>
<language id="1">
<name>C</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="2" />
<intendedUse id="3" />
<intendedUse id="4" />
</purpose>
<features>
<feature id="1" />
<feature id="4" />
</features>
<standards>
<standard id="1" />
<standard id="2" />
</standards>
</language>
<language id="2">
<name>C++</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="2" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="3" />
<feature id="4" />
<feature id="5" />
</features>
<standards>
<standard id="2" />
</standards>
</language>
<language id="3">
<name>C#</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="5" />
<intendedUse id="6" />
<intendedUse id="7" />
<intendedUse id="3" />
<intendedUse id="8" />
<intendedUse id="9" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="3" />
<feature id="4" />
<feature id="5" />
<feature id="6" />
<feature id="7" />
</features>
<standards>
<standard id="3" />
<standard id="2" />
</standards>
</language>
<language id="4">
<name>Java</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="6" />
<intendedUse id="7" />
<intendedUse id="3" />
<intendedUse id="8" />
<intendedUse id="9" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="5" />
<feature id="6" />
</features>
<standards>
<standard id="4" />
</standards>
</language>
<language id="5">
<name>JavaScript</name>
<purpose>
<intendedUse id="7" />
<intendedUse id="8" />
<intendedUse id="7" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="3" />
<feature id="6" />
</features>
<standards>
<standard id="3" />
</standards>
</language>
<language id="6">
<name>PHP</name>
<purpose>
<intendedUse id="8" />
<intendedUse id="9" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="4" />
<feature id="6" />
</features>
<standards>
<standard id="5" />
</standards>
</language>
<language id="7">
<name>Python</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="3" />
<intendedUse id="9" />
<intendedUse id="10" />
<intendedUse id="11" />
<intendedUse id="12" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="3" />
<feature id="6" />
</features>
<standards>
<standard id="5" />
</standards>
</language>
<language id="8">
<name>Basic</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="13" />
</purpose>
<features>
<feature id="1" />
<feature id="4" />
</features>
<standards>
<standard id="1" />
<standard id="2" />
</standards>
</language>
<language id="9">
<name>ADA</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="2" />
<intendedUse id="14" />
<intendedUse id="15" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="4" />
<feature id="5" />
</features>
<standards>
<standard id="1" />
<standard id="2" />
<standard id="6" />
</standards>
</language>
<language id="10">
<name>ActionScript 3.0</name>
<purpose>
<intendedUse id="1" />
<intendedUse id="7" />
<intendedUse id="9" />
</purpose>
<features>
<feature id="1" />
<feature id="2" />
<feature id="7" />
</features>
<standards>
<standard id="3" />
</standards>
</language>
</languages>
<purpose>
<intendedUse id="1"> Application</intendedUse>
<intendedUse id="2"> System </intendedUse>
<intendedUse id="3"> General Purpose </intendedUse>
<intendedUse id="4"> Low-Level Operations </intendedUse>
<intendedUse id="5"> RAD </intendedUse>
<intendedUse id="6"> Business </intendedUse>
<intendedUse id="7"> Client-Side </intendedUse>
<intendedUse id="8"> Server-Side </intendedUse>
<intendedUse id="9"> Web </intendedUse>
<intendedUse id="10"> Scripting </intendedUse>
<intendedUse id="11"> AI </intendedUse>
<intendedUse id="12"> Scientific Computing </intendedUse>
<intendedUse id="13">Education</intendedUse>
<intendedUse id="14">Embedded</intendedUse>
<intendedUse id="15">RealTime</intendedUse>
</purpose>
<features>
<feature id="1">Imperative </feature>
<feature id="2">Object-Oriented</feature>
<feature id="3">Functional</feature>
<feature id="4">Procedural</feature>
<feature id="5">Generic</feature>
<feature id="6">Reflective</feature>
<feature id="7">Event-Driven</feature>
</features>
<standards>
<standard id="1">ANSI</standard>
<standard id="2">ISO</standard>
<standard id="3">ECMA</standard>
<standard id="4">DE FACTO</standard>
<standard id="5">NO</standard>
<standard id="6">GOST</standard>
</standards>
</programming>
打印出来的是
=======编程语言1 =======
语言:C
预期用途:应用程序应用程序应用程序 势在必行 势在必行
=======编程语言2 =======
语言:C ++
预期用途:申请表
势在必行 势在必行 势在必行 势在必行 势在必行
答案 0 :(得分:1)
第一件事:
<xsl:for-each select="/programming/purpose/intendedUse[@id=$intendedUseID]" >
将您置于intendedUse
的上下文中。从这个上下文中,您只需要通过执行以下操作来获取当前节点的值:
<xsl:value-of select="." />
你拥有它的方式:
<xsl:value-of select="/programming/purpose/intendedUse" />
返回根目录并选择此路径上的第一个节点。
第二件事是,当您的第一个也是唯一一个模板在输入树的中间开始时,更高层次的节点及其后代将由built-in template rules处理。
因此,您将在结果中累积大量垃圾文本,因为默认情况下,所有文本节点都会复制到输出树中。您应该使第一个模板与根节点匹配:
<xsl:template match="/">
并将包含的路径更改为从那里开始。
最后,您应该考虑使用keys来查找文档其他分支的数据:它更整洁,更高效。