2014-09-22 126 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