Getting xslt of the two xml's
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..开发者_JAVA百科..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>
Please get the answer.xml using xslt from the using one.xml and two.xml matching one element exist in both ie one.xml having <id type='integer'>67431502</id>
which match with the <todo-item-id type="integer">67431502</todo-item-id>
of two.xml to get the answer.xml
This transformation:
<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>
when applied on the provided XML document:
<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>
produces the wanted, correct answer:
<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>
Explanation:
For convenience the second XML document is embedded in the stylesheet. In any practical implementation it will reside in its own file -- this will cause only the argument to the
document()
function calls to be replaced with the particular file URL.The identity rule/template copies every node "as-is".
The identity rule is overriden by a single template matching those
todo-item
elements the value of whoseid
child is the same as the value of theid
child of sometime-entry
element in the second document.For all such elements the processing is as with the identity template, but an additional child is added after the current children -- this is the
description
child of the correspondingtime-entry
from the second document.
With keys, this stylesheet:
<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>
Output:
<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>
Note: key()
XSLT function works against the context node's document.
精彩评论