获取两个xml的xslt

时间:2011-03-18 11:04:49

标签: xslt

One.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

<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>akumar@tekege.com</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>rkumar@tekege.com</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>rkumar@tekege.com</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从使用one.xml获取answer.xml,并且两个匹配一个元素的两个文件都存在,即one.xml,其中<id type='integer'>67431502</id>与two.xml的<todo-item-id type="integer">67431502</todo-item-id>匹配获取answer.xml

2 个答案:

答案 0 :(得分: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>akumar@tekege.com</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>rkumar@tekege.com</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>rkumar@tekege.com</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子元素的值相同第二个文档中的{1}}元素。

  4. 对于所有此类元素,处理与身份模板一样,但在当前子项之后添加了一个额外的子项 - 这是来自相应time-entry的{​​{1}}子项第二份文件。

答案 1 :(得分: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函数对上下文节点的文档起作用。