2011-03-18 70 views
0

One.xml获取两个XML的

<?xml version='1.0' encoding='ISO-8859-1'?> 
<todo-lists type='array'> 
    <todo-list> 
      <id type='integer'>10663712</id> 
      <name>Pyramid</name> 
      <todo-items type='array'> 
       <todo-item> 
        <id type='integer'>67431502</id> 
        <content>General Items that you are working on. Enter brief description 
        on what you worked on.</content> 
       </todo-item> 
       <todo-item> 
        <id type='integer'>78230534</id> 
        <content>Schedule FTPExport checking on Production.</content> 
       </todo-item> 
       <todo-item> 
        <id type='integer'>78230579</id> 
        <content>Adding Smartphone and MobileHandsets Sector on DEV</content> 
       </todo-item> 
       <todo-item> 
        <id type='integer'>78610242</id> 
        <content>Adding new Sectors on DEV.</content> 
       </todo-item> 
      </todo-items> 
    <todo-list> 
</todo-lists> 

two.xml的XSLT

<time-entries> 
    <time-entry> 
     <date type="date">2011-02-28</date> 
     <description>Learn Ajax,Webservices,JSON in Javascript</description> 
     <hours type="float">8.0</hours> 
     <id type="integer">35458966</id> 
     <person-id type="integer">6557642</person-id> 
     <email-address>[email protected]</email-address> 
     <project-id type="integer">1802011</project-id> 
     <todo-item-id type="integer">67431502</todo-item-id> 
    </time-entry> 
    <time-entry> 
     <date type="date">2011-02-28</date> 
     <description>for testing purposes... Ranjeet</description> 
     <hours type="float">1.25</hours> 
     <id type="integer">35380151</id> 
     <person-id type="integer">5949975</person-id> 
     <email-address>[email protected]</email-address> 
     <project-id type="integer">1802011</project-id> 
     <todo-item-id type="integer" nil="true"/> 
    </time-entry> 
    <time-entry> 
     <date type="date">2011-02-28</date> 
     <description>For Testing purposes....Ranjeet</description> 
     <hours type="float">1.01667</hours> 
     <id type="integer">35380081</id> 
     <person-id type="integer">5949975</person-id> 
     <email-address>[email protected]</email-address> 
     <project-id type="integer">1802011</project-id> 
     <todo-item-id type="integer" nil="true">78230534</todo-item-id> 
    </time-entry> 
</time-entries> 

answer.xml

<?xml version='1.0' encoding='ISO-8859-1'?> 
<todo-lists type='array'> 
    <todo-list> 
      <id type='integer'>10663712</id> 
      <name>Pyramid</name> 
      <todo-items type='array'> 
       <todo-item> 
        <id type='integer'>67431502</id> 
        <content>General Items that you are working on. Enter brief description 
        on what you worked on.</content> 
        <description>Learn Ajax,Webservices,JSON in Javascript</description> 
       </todo-item> 
       <todo-item> 
        <id type='integer'>78230534</id> 
        <content>Schedule FTPExport checking on Production.</content> 
        <description>For Testing purposes....Ranjeet</description> 
       </todo-item> 
       <todo-item> 
        <id type='integer'>78230579</id> 
        <content>Adding Smartphone and MobileHandsets Sector on DEV</content> 
       </todo-item> 
       <todo-item> 
        <id type='integer'>78610242</id> 
        <content>Adding new Sectors on DEV.</content> 
       </todo-item> 
      </todo-items> 
    <todo-list> 
</todo-lists> 

使用XSLT从使用请拿到answer.xml one.xml和two.xml中存在匹配一个元素的ie文件one.xml中有<id type='integer'>67431502</id>,这两个文件与two.xml的<todo-item-id type="integer">67431502</todo-item-id>相匹配以获得a nswer.xml

+0

好问题,+1。查看我的答案,获得基于最基本的XSLT设计模式的完整,简短且容易的XSLT解决方案 - 使用和覆盖身份规则。还提供了广泛的解释。 – 2011-03-18 13:01:58

回答

1

这种转变

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="my:my" > 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<my:doc2> 
    <time-entries> 
     <time-entry> 
      <date type="date">2011-02-28</date> 
      <description>Learn Ajax,Webservices,JSON in Javascript</description> 
      <hours type="float">8.0</hours> 
      <id type="integer">35458966</id> 
      <person-id type="integer">6557642</person-id> 
      <email-address>[email protected]</email-address> 
      <project-id type="integer">1802011</project-id> 
      <todo-item-id type="integer">67431502</todo-item-id> 
     </time-entry> 
     <time-entry> 
      <date type="date">2011-02-28</date> 
      <description>for testing purposes... Ranjeet</description> 
      <hours type="float">1.25</hours> 
      <id type="integer">35380151</id> 
      <person-id type="integer">5949975</person-id> 
      <email-address>[email protected]</email-address> 
      <project-id type="integer">1802011</project-id> 
      <todo-item-id type="integer" nil="true"/> 
     </time-entry> 
     <time-entry> 
      <date type="date">2011-02-28</date> 
      <description>For Testing purposes....Ranjeet</description> 
      <hours type="float">1.01667</hours> 
      <id type="integer">35380081</id> 
      <person-id type="integer">5949975</person-id> 
      <email-address>[email protected]</email-address> 
      <project-id type="integer">1802011</project-id> 
      <todo-item-id type="integer" nil="true">78230534</todo-item-id> 
     </time-entry> 
    </time-entries> 
</my:doc2> 

<xsl:variable name="vDoc2" select="document('')/*/my:doc2"/> 

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

<xsl:template match="todo-item[id = document('')/*/my:doc2/*/*/id]"> 
    <xsl:copy> 
    <xsl:apply-templates select= 
    "node()|@*|$vDoc2/*/*[id = current()/id]/description"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

时所提供的XML文档应用:

<todo-lists type='array'> 
    <todo-list> 
     <id type='integer'>10663712</id> 
     <name>Pyramid</name> 
     <todo-items type='array'> 
      <todo-item> 
       <id type='integer'>67431502</id> 
       <content>General Items that you are working on. Enter brief description 
       on what you worked on.</content> 
      </todo-item> 
      <todo-item> 
       <id type='integer'>78230534</id> 
       <content>Schedule FTPExport checking on Production.</content> 
      </todo-item> 
      <todo-item> 
       <id type='integer'>78230579</id> 
       <content>Adding Smartphone and MobileHandsets Sector on DEV</content> 
      </todo-item> 
      <todo-item> 
       <id type='integer'>78610242</id> 
       <content>Adding new Sectors on DEV.</content> 
      </todo-item> 
     </todo-items> 
    </todo-list> 
</todo-lists> 

产生想要的,正确的答案:

<todo-lists type="array"> 
    <todo-list> 
     <id type="integer">10663712</id> 
     <name>Pyramid</name> 
     <todo-items type="array"> 
     <todo-item> 
      <id type="integer">67431502</id> 
      <content>General Items that you are working on. Enter brief description 
       on what you worked on.</content> 
     </todo-item> 
     <todo-item> 
      <id type="integer">78230534</id> 
      <content>Schedule FTPExport checking on Production.</content> 
     </todo-item> 
     <todo-item> 
      <id type="integer">78230579</id> 
      <content>Adding Smartphone and MobileHandsets Sector on DEV</content> 
     </todo-item> 
     <todo-item> 
      <id type="integer">78610242</id> 
      <content>Adding new Sectors on DEV.</content> 
     </todo-item> 
     </todo-items> 
    </todo-list> 
</todo-lists> 

说明

  1. 为了方便起见,第二个XML文档被嵌入在样式表。在任何实际的实现中,它将驻留在它自己的文件中 - 这将导致只有document()函数调用的参数被替换为特定的文件URL。

  2. 身份规则/模板“按原样”复制每个节点。

  3. 身份规则由单个模板匹配那些todo-item元件,其id子的值重写是一样的id子一些time-entry元件中的所述第二文档中的值。

  4. 对于所有这样的元件的处理是作为与身份模板,但附加的孩子当前孩子后加入 - 这是从第二文档的description孩子相应time-entry的。

0

的钥匙,这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:key name="kDescriptionById" 
      match="description" 
      use="../todo-item-id"/> 
    <xsl:variable name="vSource2" select="document('two.xml')"/> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="todo-item"> 
     <xsl:variable name="vCurrent" select="."/> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
      <xsl:for-each select="$vSource2"> 
       <xsl:apply-templates 
       select="key('kDescriptionById',$vCurrent/id)"/> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

输出:

<todo-lists type="array"> 
    <todo-list> 
     <id type="integer">10663712</id> 
     <name>Pyramid</name> 
     <todo-items type="array"> 
      <todo-item> 
       <id type="integer">67431502</id> 
       <content>General Items that you are working on. Enter brief description      on what you worked on.</content> 
       <description>Learn Ajax,Webservices,JSON in Javascript</description> 
      </todo-item> 
      <todo-item> 
       <id type="integer">78230534</id> 
       <content>Schedule FTPExport checking on Production.</content> 
       <description>For Testing purposes....Ranjeet</description> 
      </todo-item> 
      <todo-item> 
       <id type="integer">78230579</id> 
       <content>Adding Smartphone and MobileHandsets Sector on DEV</content> 
      </todo-item> 
      <todo-item> 
       <id type="integer">78610242</id> 
       <content>Adding new Sectors on DEV.</content> 
      </todo-item> 
     </todo-items> 
    </todo-list> 
</todo-lists> 

注意key() XSLT功能的工作原理对上下文节点的文档。

+0

但answer.xml显示每个内容下面的所有描述文本,而不是根据匹配的内容。xml具有 67431502,它们与two.xml中的 67431502匹配以获取answer.xml – satya 2011-03-20 01:55:51

+0

@satya :我不明白你的抱怨。是不是你提供的完全相同的期望输出? – 2011-03-20 18:30:25