2011-03-18 60 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