访问多个源的相同名称元素
问题描述:
我正在使用XSLT解析文档。在XSLT中,我使用document()函数加载参考文档,并且可以从两个文档中访问项目。源文档和参考文档都有一个名为name的属性。我如何比较一个和另一个。我通过声明一个变量来解决这个问题,但是如果可能的话,我宁愿这样做。在我看来,我需要将命名空间放在事物的周围,但不知道如何去做。访问多个源的相同名称元素
源文件:
<?xml version="1.0" encoding="UTF-8"?>
<SoccerMatch revision="21" id="2849180" date="20080405" scheduledStart="1245" venue="Emirates Stadium" status="Result" comment="" league="Friendly Match" attendance="60111">
<stuffhere>stuff</stuffhere>
<stuffhere>stuff</stuffhere>
<stuffhere>stuff</stuffhere>
<stuffhere>stuff</stuffhere>
<stuffhere>stuff</stuffhere>
</SoccerMatch>
参考文献(comp.xml):
<?xml version="1.0" encoding="utf-8"?>
<competitions>
<competition id="100" league="Barclays Premier League"/>
<competition id="101" league="The Coca-Cola Football League Championship"/>
<competition id="101" league="The Coca-Cola Football League Championship Play-Offs Semi-Final"/>
<competition id="101" league="The Coca-Cola Football League Championship Play-Offs Final"/>
<competition id="102" league="Coca-Cola Football League One"/>
<competition id="102" league="Coca-Cola Football League One Play-Offs Semi-Final"/>
<competition id="102" league="Coca-Cola Football League One Play-Offs Final"/>
<competition id="103" league="Coca-Cola Football League Two"/>
<competition id="103" league="Coca-Cola Football League Two Play-Offs Semi-Final"/>
<competition id="103" league="Coca-Cola Football League Two Play-Offs Final"/>
<competition id="104" league="Blue Square Premier"/>
<competition id="104" league="Blue Square Premier Play-Offs Semi-Final"/>
<competition id="104" league="Blue Square Premier Final"/>
<competition id="105" league="Nationwide Championship Shield"/>
<competition id="120" league="Clydesdale Bank Premier League"/>
<competition id="121" league="The Irn-Bru Scottish Football League Championship First Division"/>
<competition id="121" league="The Irn-Bru Scottish Football League Championship First Division Play-Offs Semi-Final"/>
<competition id="121" league="The Irn-Bru Scottish Football League Championship First Division Play-Offs Final"/>
<competition id="122" league="The Irn-Bru Scottish Football League Championship Second Division"/>
<competition id="122" league="The Irn-Bru Scottish Football League Championship Second Division Play-Offs Semi-Final"/>
<competition id="122" league="The Irn-Bru Scottish Football League Championship Second Division Play-Offs Final"/>
<competition id="123" league="The Irn-Bru Scottish Football League Championship Third Division"/>
</competitions>
XSLT
<xsl:template match="/SoccerMatch">
<xmlfeed>
<payload payload_class="mobile_football_match_details" payload_name="Payload">
<xsl:variable name="compId" select="document('comp.xml')/competitions/competition[@[email protected]]/@id" />
答
不要看到你的XML格式的 “name” 属性。你的意思是“身份证”?我不认为你可以在这里控制命名空间。请参阅Ken Holman关于这一点的讨论here。
我会去你的变量的想法。喜欢的东西:
<xsl:template match="/SoccerMatch">
<xsl:variable name="matchId" select="@id"/>
<xsl:for-each select="document('competitions.xml')/competitions/competition">
<xsl:if test="$matchId = @id">
<xmlfeed>
<payload payload_class="mobile_football_match_details" payload_name="Payload">
<!-- add more stuff here-->
</payload>
</xmlfeed>
</xsl:if>
</xsl:for-each>