簡體   English   中英

XSLT映射父節點的不同子節點

[英]XSLT map different child of a parent node

我最近開始使用XSLT。 我正在嘗試獲取此xml集的報告

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(document($ filename)),因為有100多個文件,其中有數百萬行可供查找

請指教。

如果您更換

                <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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM