XPATH/XSLT:选择父节点的属性与另一个节点的属性匹配的节点 [英] XPATH / XSLT: Selecting a node where the parent node's attribute matches the attribute of another node

查看:26
本文介绍了XPATH/XSLT:选择父节点的属性与另一个节点的属性匹配的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XPath 和 XSLT 对以下 XML 应用转换.

I'm trying to apply a transformation to the below XML using XPath and XSLT.

<AuthorGroup>
  <Author AffiliationID="A1">
    <GivenName>John</GivenName>
    <Initials/>
    <FamilyName>Smith</FamilyName>
    <Degrees/>
    <Roles/>
  </Author>
    <Author AffiliationID="A2">
    <GivenName>Bill</GivenName>
    <Initials/>
    <FamilyName>Atkins</FamilyName>
    <Degrees/>
    <Roles/>
  </Author>
  <Affiliation AFFID="A1">
    <OrgDivision/>
    <OrgName>Some Org</OrgName>
    <OrgAddress/>
  </Affiliation>
  <Affiliation AFFID="A2">
    <OrgDivision/>
    <OrgName>Another Org</OrgName>
    <OrgAddress/>
  </Affiliation>
</AuthorGroup>

我试图将作者和单位名称组合在一个节点下,所以我最终会得到如下结构:

I'm trying to combine the author and affiliation names under one node so I'll end up with a structure like:

<AUG>
  <AU><NAME>John Smith</NAME><AUINFO>Some Org</AUINFO></AU>
  <AU><NAME>Bill Atkins</NAME><AUINFO>Another Org</AUINFO></AU>
</AUG>

我正在努力寻找一种方法来选择一个节点的值,该节点的父节点的属性与另一个节点的属性值匹配.例如,对于作者 John Smith,我想选择关联 ID 匹配的节点的值.

I'm struggling to find a way of selecting the value of a node who's parent node's attribute matches the value of an attribute of an another node. For example, For the author John Smith I'd like to select the value of the node where the affiliation ID's match.

到目前为止,我的样式表中有一些代码,如下所示,但我知道节点内的选择不起作用.

So far I have some code in my stylesheet like the below but I know the selection inside the node is not working.

<xsl:for-each select="AuthorGroup/Author">
  <AU>
  <NAME><xsl:value-of select="./FamilyName" /> <xsl:value-of select="./GivenName" /></NAME>
  <AUINFO><xsl:value-of select="../Affiliation[@AFFID='NEED TO MATCH AUTHOR AFFILIATIONID']/OrgName" /> </AUINFO>


  </AU>         
</xsl:for-each>

我应该如何为关联作者选择正确的组织名称的值?

How should I select the value of the correct organisation name for the associated author?

非常感谢

吉姆

推荐答案

作为使用变量的替代方法,您可以使用current()"运算符来获取您所在的当前作者节点

As an alternative to using a variable, you can make use of the "current()" operator to get the current Author node you are on

<xsl:value-of select="../Affiliation[@AFFID=current()/@AffiliationID]/OrgName"/>

因此,如果您采用以下样式表

Thus, if you take the following stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <AUG>
         <xsl:for-each select="AuthorGroup/Author">
            <AU>
               <NAME>
                  <xsl:value-of select="./FamilyName"/>
                  <xsl:value-of select="./GivenName"/>
               </NAME>
               <AUINFO>
                  <xsl:value-of select="../Affiliation[@AFFID=current()/@AffiliationID]/OrgName"/>
               </AUINFO>
            </AU>
         </xsl:for-each>
      </AUG>
   </xsl:template>
</xsl:stylesheet>

并应用给定的 XML,结果应该是这样

And apply it your given XML, the results should be like so

<AUG>
   <AU>
      <NAME>SmithJohn</NAME>
      <AUINFO>Some Org</AUINFO>
   </AU>
   <AU>
      <NAME>AtkinsBill</NAME>
      <AUINFO>Another Org</AUINFO>
   </AU>
</AUG>

这篇关于XPATH/XSLT:选择父节点的属性与另一个节点的属性匹配的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆