XPATH / XSLT: Selecting a node where the parent node's attribute matches the attribute of another node

前端 未结 4 2050
野趣味
野趣味 2021-01-06 04:08

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


  
    J         


        
相关标签:
4条回答
  • 2021-01-06 04:42

    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>
    

    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>
    
    0 讨论(0)
  • 2021-01-06 04:49

    Just a quick answer ... hope this helps, can't run it myself for the moment :-S you can put the ID in a variable like:

    <xsl:for-each select="AuthorGroup/Author">
      <xsl:variable name="affID" select="@AFFID" />
      <AU>
        <NAME><xsl:value-of select="./FamilyName" /> <xsl:value-of select="./GivenName" /></NAME>
        <AUINFO><xsl:value-of select="../Affiliation[@AFFID=$affID]/OrgName" /> </AUINFO>
      </AU>         
    </xsl:for-each>
    
    0 讨论(0)
  • 2021-01-06 04:50
    <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=./@AffiliationID]/OrgName" /> 
         </AUINFO>
    


    0 讨论(0)
  • 2021-01-06 04:58

    Keys are the right tool for cross references. This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:strip-space elements="*"/>
        <xsl:key name="kAffiliationByAFFID" match="Affiliation" use="@AFFID"/>
        <xsl:template match="AuthorGroup">
            <AUG>
                <xsl:apply-templates/>
            </AUG>
        </xsl:template>
        <xsl:template match="Author">
            <AU>
                <NAME>
                    <xsl:value-of select="concat(GivenName,' ',FamilyName)"/>
                </NAME>
                <xsl:apply-templates
                     select="key('kAffiliationByAFFID',@AffiliationID)"
                     mode="out"/>
            </AU>
        </xsl:template>
        <xsl:template match="Affiliation"/>
        <xsl:template match="Affiliation" mode="out">
            <AUINFO>
                <xsl:apply-templates/>
            </AUINFO>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <AUG>
        <AU>
            <NAME>John Smith</NAME>
            <AUINFO>Some Org</AUINFO>
        </AU>
        <AU>
            <NAME>Bill Atkins</NAME>
            <AUINFO>Another Org</AUINFO>
        </AU>
    </AUG>
    
    0 讨论(0)
提交回复
热议问题