基于匹配属性,从第二个文档向XML添加节点

时间:2015-02-07 05:01:56

标签: xml xslt

我有一个包含大量精彩数据的xml文档,但现在要求我的权力包括摘要和参与者。所以问题是如何获取看起来像这样的数据:

    <mediaList>
       <movie id="1125898" dateCreated="2014-04-12" lastModified="2014-05-23">
          <title>127 Hours</title>
          <director sort="7">Danny Boyle</director>
          <genreWrap>
             <genre>Drama</genre>
             <subGenre>Dramas Based on Real Life</subGenre>
          </genreWrap>
          <genreWrap>
             <genre>Thrillers</genre>
             <subGenre>Suspense</subGenre>
          </genreWrap>
          <LCSpecialTopics>Adventure films</LCSpecialTopics>
          <writer sort="7">Simon Beaufoy</writer>
          <writer sort="7">Danny Boyle</writer>
          <screenplay href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1118107">PN1997.2 .O54 2010</screenplay>
          <language>English</language>
          <year>2010</year>
          <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1125898">[DVD] PN1995.9 .A3 B69 2011</callNumber>
          <coverArt href="Pics/127Hours.jpg"/>
       </movie>
       <movie id="1155300" dateCreated="2014-04-12" lastModified="2014-05-23">
          <title>Lord Jim</title>
          <director sort="9">Richard Brooks</director>
          <genreWrap>
             <genre>Action and Adventure</genre>
             <subGenre>Action Classics</subGenre>
             <subGenre>Adventures</subGenre>
          </genreWrap>
          <genreWrap>
             <genre>Classics</genre>
             <subGenre>Action Classics</subGenre>
          </genreWrap>
          <LCSpecialTopics>Adventure films</LCSpecialTopics>
          <writer sort="9">Richard Brooks</writer>
          <language>English</language>
          <year>1965</year>
          <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1155300">[DVD] PN1995.9 .A3 B76 2004</callNumber>
          <coverArt href="Pics/LordJim.jpg"/>
       </movie>
   <tvShow id="1686708" dateCreated="2014-12-05">
      <title differentiator="Series 7">Prime Suspect</title>
      <creator sort="10">Lynda La Plante</creator>
      <genreWrap>
         <genre>Television</genre>
         <subGenre>TV Mysteries</subGenre>
         <subGenre>TV Dramas</subGenre>
         <subGenre>Foreign TV</subGenre>
         <subSubGenre>Foreign TV Dramas</subSubGenre>
      </genreWrap>
      <LCSpecialTopics>Individual programs</LCSpecialTopics>
      <language>English</language>
      <year>2006</year>
      <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1686708">[DVD] PN1992.77 .P699 M37 2007</callNumber>
      <coverArt href="Pics/PrimeSuspect7.jpg"/>
   </tvShow>
   <tvShow id="1689302" dateCreated="2014-12-05">
      <title differentiator="The Complete Series">Profit</title>
      <creator sort="7">David Greenwalt</creator>
      <creator sort="6">John McNamara</creator>
      <genreWrap>
         <genre>Television</genre>
         <subGenre>TV Dramas</subGenre>
         <subSubGenre>TV Dramedy</subSubGenre>
      </genreWrap>
      <LCSpecialTopics>Individual programs</LCSpecialTopics>
      <language>English</language>
      <year>1996</year>
      <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1689302">[DVD] PN1992.77 .P76 2005</callNumber>
      <coverArt href="Pics/Profit.jpg"/>
   </tvShow>
    </mediaList>

然后使用源材料 - 见此处,

<root>
  <row>
        <Heading0>PN 19959           A 3   B 69   2011</Heading0>
        <Heading1>[DVD] PN1995.9.A3 B69 2011</Heading1>
        <Heading2>127 hours [videorecording] / Fox Searchlight Pictures and Pathâe present, in association with Everest Entertainment ; a Cloud Eight, Decibel Films, Darlow Smithson production ; a Danny Boyle film ; produced by Christian Colson, Danny Boyle, John Smithso</Heading2>
        <Heading3>127 hours</Heading3>
        <Heading4>127 hours (Motion picture)</Heading4>
        <Heading5></Heading5>
        <Heading6>Twentieth Century Fox Home Entertainment,</Heading6>
        <Heading7>2011</Heading7>
        <Heading8></Heading8>
        <Heading9>2011</Heading9>
        <Heading10>[2011]</Heading10>
        <Heading11>eng</Heading11>
        <Heading12>Closed-captioned.
English (5.1 Dolby Digital), dubbed French (Dolby surround) or dubbed Spanish (Dolby surround) dialogue, optional French or Spanish subtitles; English subtitles for the deaf and hard of hearing (SDH)</Heading12>
        <Heading13>Based on the book "Between a rock and a hard place" by Aron Ralston.
Videodisc release of the 2010 motion picture.
DVD release date: Mar. 1, 2011.
Special features include commentary and deleted scenes.</Heading13>
        <Heading14>James Franco, Amber Tamblyn, Kate Mara, Clâemence Poâesy, Kate Burton, Lizzy Caplan, Treat Williams.</Heading14>
        <Heading15>"... the powerfully uplifting true story of one man's struggle to survive against mountainous odds. Aron Ralston (James Franco) has a passion for all things outdoors. But when a falling boulder traps him in a remote Utah canyon, a thrill-seeker's adventure becomes the challenge of a lifetime. Over the next five days, Ralston embarks on a remarkable personal journey in which he relies on the memories of family and friends--as well as his own courage and ingenuity--to turn adversity into triumph!"--Container.</Heading15>
        <Heading16>1125898</Heading16>
        <Heading17>1413868</Heading17>
        <Heading18>1351628</Heading18>
        <Heading19>0113503003890</Heading19>
    </row>
  <row>
        <Heading0>PN 19959           A 3   B 76   2004</Heading0>
        <Heading1>[DVD] PN1995.9.A3 B76 2004</Heading1>
        <Heading2>Lord Jim [videorecording] / a film by Richard Brooks ; written for the screen and directed by Richard Brooks.</Heading2>
        <Heading3>Lord Jim</Heading3>
        <Heading4></Heading4>
        <Heading5></Heading5>
        <Heading6>Columbia Pictures ; Columbia TriStar Home Entertainment,</Heading6>
        <Heading7>2004</Heading7>
        <Heading8>1965</Heading8>
        <Heading9>2004-1965</Heading9>
        <Heading10>[2004]</Heading10>
        <Heading11>eng</Heading11>
        <Heading12>English or French dialogue, English, French, Japanese, Spanish subtitles; closed-captioned.</Heading12>
        <Heading13>Title from container.
Originally released as a motion picture in 1965.
Based on the novel by Joseph Conrad.</Heading13>
        <Heading14>Peter O'Toole, James Mason, Eli Wallach, Curt Jurgens, Paul Lukas.</Heading14>
        <Heading15>A disgraced 19th-century British sailor lives nobly among natives. From the Joseph Conrad novel.</Heading15>
        <Heading16>1155300</Heading16>
        <Heading17>1443177</Heading17>
        <Heading18>1360820</Heading18>
        <Heading19>0113503022411</Heading19>
    </row>
  <row>
        <Heading0>PN 199277          P 699   M 37   2007</Heading0>
        <Heading1>[DVD] PN1992.77.P699 M37 2007</Heading1>
        <Heading2>Prime suspect. The final act [videorecording] / a co-production of Granada and WGBH Boston ; producers, Andy Harries and Rebecca Eaton ; produced by Andrew Benson ; written by Frank Deasy ; directed by Philip Martin.</Heading2>
        <Heading3>Prime suspect.</Heading3>
        <Heading4></Heading4>
        <Heading5></Heading5>
        <Heading6>Acorn Media,</Heading6>
        <Heading7>2007</Heading7>
        <Heading8></Heading8>
        <Heading9>2007</Heading9>
        <Heading10>[2007]</Heading10>
        <Heading11>eng</Heading11>
        <Heading12>Closed-captioned.</Heading12>
        <Heading13>Originally broadcast on British television in 2006.
Special features: Fifty-minute behind-the-scenes featurette; photo gallery; cast filmographies.</Heading13>
        <Heading14>Helen Mirren, Frank Finlay, Tom Bell, Laura Greenwood.</Heading14>
        <Heading15>Detective Superintendent Jane Tennison finds herself searching for Sallie Sturdy, a fourteen-year-old girl presumed murdered. Facing the hospitalization of her father, descending deeper into alcoholism, and resisting pressure to retire quietly, Tennison vows to bring Sallie's killer to justice before ending her career. She forges a surprising friendship with Penny Philips, Sallie's best friend.</Heading15>
        <Heading16>1686708</Heading16>
        <Heading17>2104396</Heading17>
        <Heading18>1500951</Heading18>
        <Heading19>0113503133952</Heading19>
    </row>
    <row>
        <Heading0>PN 199277          P 76   2005</Heading0>
        <Heading1>[DVD] PN1992.77 .P76 2005</Heading1>
        <Heading2>Profit. The complete series [videorecording] / Greenwalt McNamara Productions ; a Stephen J. Cannell Production ; created by David Greenwalt and John McNamara ; executive producers, David Greenwalt and John McNamara ; produced by John B. Moranville.</Heading2>
        <Heading3>Profit.</Heading3>
        <Heading4></Heading4>
        <Heading5></Heading5>
        <Heading6>Anchor Bay Entertainment,</Heading6>
        <Heading7>2005</Heading7>
        <Heading8>1996</Heading8>
        <Heading9>2005-1996</Heading9>
        <Heading10>c2005.</Heading10>
        <Heading11>eng</Heading11>
        <Heading12>Closed-captioned.</Heading12>
        <Heading13>Title from container.
Originally aired on television in 1996.
Special features include interviews and commentaries.</Heading13>
        <Heading14>Adrian Pasdar, Sherman Augustus, Lisa Zane, Lisa Blount, Lisa Darr, Jack Gwaltney, Allison Hossack, Keith Szarabajka.</Heading14>
        <Heading15>A charismatic sociopath turns a childhood secret into a ruthless climb up the corporate ladder.</Heading15>
        <Heading16>1689302</Heading16>
        <Heading17>2106987</Heading17>
        <Heading18>1501141</Heading18>
        <Heading19>0113503134315</Heading19>
    </row>
</root>

输出结果如下:

    <mediaList>
       <movie id="1125898" dateCreated="2014-04-12" lastModified="2014-05-23">
          <title>127 Hours</title>
          <director sort="7">Danny Boyle</director>
          <genreWrap>
             <genre>Drama</genre>
             <subGenre>Dramas Based on Real Life</subGenre>
          </genreWrap>
          <genreWrap>
             <genre>Thrillers</genre>
             <subGenre>Suspense</subGenre>
          </genreWrap>
          <summary>"... the powerfully uplifting true story of one man's struggle to survive against mountainous odds. Aron Ralston (James Franco) has a passion for all things outdoors. But when a falling boulder traps him in a remote Utah canyon, a thrill-seeker's adventure becomes the challenge of a lifetime. Over the next five days, Ralston embarks on a remarkable personal journey in which he relies on the memories of family and friends--as well as his own courage and ingenuity--to turn adversity into triumph!"</summary>
          <LCSpecialTopics>Adventure films</LCSpecialTopics>
          <writer sort="7">Simon Beaufoy</writer>
          <writer sort="7">Danny Boyle</writer>
          <screenplay href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1118107">PN1997.2 .O54 2010</screenplay>
          <language>English</language>
          <year>2010</year>
          <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1125898">[DVD] PN1995.9 .A3 B69 2011</callNumber>
          <coverArt href="Pics/127Hours.jpg"/>
       </movie>
       <movie id="1155300" dateCreated="2014-04-12" lastModified="2014-05-23">
          <title>Lord Jim</title>
          <director sort="9">Richard Brooks</director>
          <genreWrap>
             <genre>Action and Adventure</genre>
             <subGenre>Action Classics</subGenre>
             <subGenre>Adventures</subGenre>
          </genreWrap>
          <genreWrap>
             <genre>Classics</genre>
             <subGenre>Action Classics</subGenre>
          </genreWrap>
          <summary>A disgraced 19th-century British sailor lives nobly among natives. From the Joseph Conrad novel.</summary>
          <LCSpecialTopics>Adventure films</LCSpecialTopics>
          <writer sort="9">Richard Brooks</writer>
          <language>English</language>
          <year>1965</year>
          <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1155300">[DVD] PN1995.9 .A3 B76 2004</callNumber>
          <coverArt href="Pics/LordJim.jpg"/>
       </movie>
   <tvShow id="1686708" dateCreated="2014-12-05">
      <title differentiator="Series 7">Prime Suspect</title>
      <creator sort="10">Lynda La Plante</creator>
      <genreWrap>
         <genre>Television</genre>
         <subGenre>TV Mysteries</subGenre>
         <subGenre>TV Dramas</subGenre>
         <subGenre>Foreign TV</subGenre>
         <subSubGenre>Foreign TV Dramas</subSubGenre>
      </genreWrap>
      <summary>Detective Superintendent Jane Tennison finds herself searching for Sallie Sturdy, a fourteen-year-old girl presumed murdered. Facing the hospitalization of her father, descending deeper into alcoholism, and resisting pressure to retire quietly, Tennison vows to bring Sallie's killer to justice before ending her career. She forges a surprising friendship with Penny Philips, Sallie's best friend.</summary>
      <LCSpecialTopics>Individual programs</LCSpecialTopics>
      <language>English</language>
      <year>2006</year>
      <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1686708">[DVD] PN1992.77 .P699 M37 2007</callNumber>
      <coverArt href="Pics/PrimeSuspect7.jpg"/>
   </tvShow>
   <tvShow id="1689302" dateCreated="2014-12-05">
      <title differentiator="The Complete Series">Profit</title>
      <creator sort="7">David Greenwalt</creator>
      <creator sort="6">John McNamara</creator>
      <genreWrap>
         <genre>Television</genre>
         <subGenre>TV Dramas</subGenre>
         <subSubGenre>TV Dramedy</subSubGenre>
      </genreWrap>
      <summary>A charismatic sociopath turns a childhood secret into a ruthless climb up the corporate ladder.</summary>
      <LCSpecialTopics>Individual programs</LCSpecialTopics>
      <language>English</language>
      <year>1996</year>
      <callNumber href="http://endeavor.flo.org/vwebv/holdingsInfo?bibId=1689302">[DVD] PN1992.77 .P76 2005</callNumber>
      <coverArt href="Pics/Profit.jpg"/>
   </tvShow>
    </mediaList>

每个电影或电视剧(在第一个文档中)都有一个@id属性,它与第二个文档中的Heading16元素匹配,当找到匹配时,Heading15应该插入到最终genreWrap元素之后的movie或tvShow元素中在摘要元素中。任何帮助都是非常有用的。有电影或tvShow元素具有相同的@id属性,因为它们是一个盒子集,以防万一。

编辑-------

我正在使用XSLT 2.0

这是我到目前为止所尝试的内容 - 我还没有弄清楚如何将文件2的特定部分放入摘要节点。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>


    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>


    <xsl:template name="main">
        <xsl:for-each-group
            select="(document('File1.xml')/mediaList/*, document('File2.xml')/root/row)"
            group-by="@id, Heading16">
            <xsl:copy-of select="current-group()"/>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="genreWrap">
        <xsl:copy-of select="."/>
            <summary>
            </summary>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

我只是利用传入文档的XSLT 2.0 key函数功能来应用密钥,如

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="doc2-url" select="'test2015020703.xml'"/>
    <xsl:variable name="doc2" select="document($doc2-url)"/>

    <xsl:key name="id" match="row" use="Heading16"/>

    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="genreWrap">
      <xsl:next-match/>
      <xsl:apply-templates select="key('id', ../@id, $doc2)/Heading15"/>
    </xsl:template>

    <xsl:template match="Heading15">
      <summary>
        <xsl:apply-templates/>
      </summary>
    </xsl:template>

</xsl:stylesheet>

提供第一个文档作为主要输入文档,第二个文档作为参数。