获取两个 xml 的 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>[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 从使用 one.xml 和 Two.xml 中获取answer.xml,其中 one.xml 和 Two.xml 匹配一个元素存在于两者中,即 one.xml 具有
与two.xml的
匹配得到答案.xml
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>[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>
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
应用于提供的 XML 文档时:
生成所需的正确答案:
说明:
为了方便起见,第二个 XML 文档嵌入在样式表中。在任何实际的实现中,它将驻留在自己的文件中——这只会导致
document()
函数调用的参数被替换为特定的文件 URL。身份规则/模板“按原样”复制每个节点。
身份规则由与那些
todo-item
元素匹配的单个模板覆盖,这些元素的id
子元素的值与id 的值相同
第二个文档中某个time-entry
元素的子元素。对于所有此类元素,处理过程与身份模板相同,但在当前子元素之后添加了一个额外的子元素 - 这是相应
time-entry< 的
description
子元素。 /code> 来自第二个文档。This transformation:
when applied on the provided XML document:
produces the wanted, correct answer:
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.对于键,此样式表:
输出:
注意:
key()
XSLT 函数针对上下文节点的文档起作用。With keys, this stylesheet:
Output:
Note:
key()
XSLT function works against the context node's document.