2014-09-22 124 views
0

我最近開始使用XSLT。我試圖讓這個XML的報告設定XSLT映射父節點的不同子節點

一個XML看起來像下面的一個並命名(company_a.xml)

<company title="abc" > 
<section> 
<in-proc uid="HR.xml"> 
    <details> 
     <empname>abcd1</empname> 
     <id>xyz1</id> 
     <empname>abcd2</empname> 
     <id>xyz2</id> 
     <empname>abcd3</empname> 
     <id>xyz3</id> 
    </details> 
</in-proc>  
<out-proc uid="manufacturing.xml"> 
    <title>any</title> 
    <details> 
     <empname>abcd4</empname> 
     <id>xyz4</id> 
    </details> 
</out-proc> 
</section> 
</company> 

使用XSL是

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes"/> 
    <xsl:template match="/"> 
    <xsl:result-document href="Emp_data.csv"> 
      <xsl:for-each select="//file"> 
       <xsl:variable name="filename" select="."/> 
       <xsl:variable name="fileid" select="substring-before(., '.xml')"/> 
       <xsl:for-each select="document($filename)"> 
        <xsl:for-each select="company/section/*/details"> 
         <xsl:variable name="business" select="local-name(..)"/> 
         <xsl:variable name="dept" select="substring-before(ancestor::*/@uid, '.xml')"/> 
          <xsl:value-of select="concat('&quot;', id , '&quot;,&quot;', empname , '&quot;,&quot;', $fileid, '&quot;,&quot;', $dept, '&quot;,&quot;', $business, '&quot;&#xA;')"/> 
        </xsl:for-each> 
       </xsl:for-each> 
      </xsl:for-each> 
    </xsl:result-document> 
    </xsl:template> 
</xsl:stylesheet> 

是給

xyz1,abcd1,company_a,HR,in-proc 
xyz4,abcd4,company_a,manufacturing,out-proc 

結果我要找的是

xyz1,abcd1,company_a,HR,in-proc 
xyz2,abcd2,company_a,HR,in-proc 
xyz3,abcd3,company_a,HR,in-proc 
xyz4,abcd4,company_a,manufacturing,out-proc 

我嘗試使用

<xsl:for-each select="id"> 
    <xsl:value-of select="."/> 

但當時我不知道如何獲取empname

我使用file_list中(文件($文件名)),因爲有超過100個文件,其中有幾百萬的線看

請指教。

+0

您的樣式表聲明XSLT版本1.0,但您僅使用XSLT 2.0'xsl:result-document'元素? – 2014-09-22 13:29:16

+0

好抓!在我的努力獲得解決方案,我改變了這一點,但忘了恢復。我正在使用XSLT 2.0 – nexusEnthu 2014-09-22 14:08:36

回答

0

如果更換

   <xsl:for-each select="company/section/*/details"> 
        <xsl:variable name="business" select="local-name(..)"/> 
        <xsl:variable name="dept" select="substring-before(ancestor::*/@uid, '.xml')"/> 
         <xsl:value-of select="concat('&quot;', id , '&quot;,&quot;', empname , '&quot;,&quot;', $fileid, '&quot;,&quot;', $dept, '&quot;,&quot;', $business, '&quot;&#xA;')"/> 
       </xsl:for-each> 

   <xsl:for-each select="company/section/*/details/empname"> 
        <xsl:variable name="business" select="local-name(../..)"/> 
        <xsl:variable name="dept" select="substring-before(ancestor::*/@uid, '.xml')"/> 
         <xsl:value-of select="concat('&quot;', following-sibling::id[1] , '&quot;,&quot;', . , '&quot;,&quot;', $fileid, '&quot;,&quot;', $dept, '&quot;,&quot;', $business, '&quot;&#xA;')"/> 
       </xsl:for-each> 

那麼你應該得到的線路爲每個現有empname

+0

非常棒!感謝Martin – nexusEnthu 2014-09-22 13:47:15