获取两个 xml 的 xslt

发布于 2024-10-24 05:50:45 字数 5159 浏览 1 评论 0原文

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 具有 ;67431502 与two.xml的67431502匹配得到答案.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2024-10-31 05:50:45

此转换

<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. 对于所有此类元素,处理过程与身份模板相同,但在当前子元素之后添加了一个额外的子元素 - 这是相应 time-entry< 的 description 子元素。 /code> 来自第二个文档。

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>[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>

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:

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

  2. The identity rule/template copies every node "as-is".

  3. The identity rule is overriden by a single template matching those todo-item elements the value of whose id child is the same as the value of the id child of some time-entry element in the second document.

  4. 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 corresponding time-entry from the second document.

鲜血染红嫁衣 2024-10-31 05:50:45

对于键,此样式表:

<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 函数针对上下文节点的文档起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文