xml - 访问多个源的同名元素

标签 xml xslt

我正在使用 XSLT 解析文档。在 XSLT 中,我使用 document() 函数加载引用文档,并且可以访问两个文档中的项目。源文档和引用文档都有一个名为名称的属性。我如何将其中一个与另一个进行比较。我通过声明一个变量来解决这个问题,但如果可能的话,我宁愿不这样做。在我看来,我需要在事物周围放置命名空间,但不知道如何去做。

源文档:

<?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[@league=@league]/@id" />

最佳答案

在您的 xml 中看不到“name”属性。您指的是 “id”吗?我认为您无法在这里控制 namespace 。参见 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>

关于xml - 访问多个源的同名元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/337621/

相关文章:

jsp - 如何使用 XSLT 或 JSP 检索网站的收藏夹图标?

xml - 基于属性值创建父子元素并抑制输出中的重复元素

css - 通过 CSS 操作 xslt 菜单

c# - 在 XML 异常处理期间,如何获取导致异常的 XML 文件中的行号?

ruby-on-rails - 如何将 Ruby 散列转换为 XML?

xml - 如何检查 XSLT 中是否存在值

xslt - 使用 ANSI 编码输出文本文件

c++ - 使用boost解析xml

xml - 使用Groovy标记构建器语法对Grails中的嵌套元素进行XML编码

xml - 如何使用 XSLT 输出重复元素?