简体   繁体   English

使用 XSLT 将具有相同 ID 的元素 (XML) 合并到 txt 文件

[英]Merge Elements (XML) With Same ID to txt file using XSLT

I have a task where I need to loop through an XML document and merge elements/nodes with the same ID.我有一个任务,我需要遍历 XML 文档并合并具有相同 ID 的元素/节点。 The output should be a csv file (for further processing) where each line have a fixed length. output 应该是 csv 文件(用于进一步处理),其中每行具有固定长度。 Based on the values of the nodes, that value needs to be placed at a certain location in the output.根据节点的值,该值需要放置在 output 中的某个位置。

Here is a sample of the XML :这是XML 的示例

<root>
    <User>
        <UserID>55555</UserID>
        <Value>Active</Value>
    </User>
    <User>
        <UserID>55555</UserID>
        <Value>Admin</Value>
    </User>
    <User>
        <UserID>55555</UserID>
        <Value>Eligible</Value>
    </User>
    <User>
        <UserID>123456</UserID>
        <Value>Active</Value>
    </User>
</root>

My desired output would be:想要的 output将是:

User ID, Active, Admin, Eligible
55555, Y, Y, Y,
123456, Y, N, N,

NOTE the values are ALWAYS THE SAME (Active, Admin & Eligible), but Users can have different amount of values like in the example.请注意,这些值始终相同(活动、管理员和合格),但用户可以有不同数量的值,如示例中所示。

Currently this is what I got:目前这是我得到的:

    <xsl:template match="/root">
        <Header>
            <xsl:text>User ID</xsl:text>
            <xsl:value-of select="$comma"/>
            
            <xsl:text>Active</xsl:text>
            <xsl:value-of select="$comma"/>
            
            <xsl:text>Admin</xsl:text>
            <xsl:value-of select="$comma"/>
            
            <xsl:text>Eligible</xsl:text>
            <xsl:text>&#xa;</xsl:text>
        </Header>
            <xsl:for-each-group select="User" group-by="UserID"> 
                
                <!-- User ID -->
                <xsl:value-of select="UserID"/>
                <xsl:value-of select="$comma"/>
                
                <xsl:for-each-group select="current-group()" group-by="Value">
                    <xsl:value-of select="current-grouping-key()"/>
                    <xsl:value-of select="$comma"/>
                </xsl:for-each-group>
                
                <xsl:value-of select="$lineFeed"/>
            </xsl:for-each-group>
    </xsl:template>

This group and selects the correct elements, but then I need to place them under the correct headers (like axample with desired output).该组并选择正确的元素,但随后我需要将它们放在正确的标题下(例如具有所需输出的 axample)。

Can anyone point me in the right direction here?谁能在这里指出我正确的方向? Any help would be much appreciated.任何帮助将非常感激。

If I understand this correctly (very big if,): you want to do something like:如果我理解正确(如果,非常大):你想做类似的事情:

XSLT 2.0 XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/root">
    <!-- header -->
    <xsl:text>User ID, Active, Admin, Eligible&#10;</xsl:text>
    <!-- rows -->
    <xsl:for-each-group select="User" group-by="UserID"> 
        <!-- User ID -->
        <xsl:value-of select="UserID"/>
        <xsl:text>, </xsl:text>
        <!-- Active -->
        <xsl:value-of select="if (current-group()/Value[.='Active']) then 'Y' else'N'"/>
        <xsl:text>, </xsl:text>
        <!-- Admin -->
        <xsl:value-of select="if (current-group()/Value[.='Admin']) then 'Y' else'N'"/>
        <xsl:text>, </xsl:text>
        <!-- Eligible -->
        <xsl:value-of select="if (current-group()/Value[.='Eligible']) then 'Y' else'N'"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

Or more compactly:或者更简洁:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/root">
    <!-- header -->
    <xsl:text>User ID, Active, Admin, Eligible&#10;</xsl:text>
    <!-- rows -->
    <xsl:for-each-group select="User" group-by="UserID"> 
        <xsl:value-of select="UserID, for $t in ('Active', 'Admin', 'Eligible') return if (current-group()/Value[.=$t]) then 'Y' else 'N'" separator=", "/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

Note that the result is slightly different from the one you posted: there is no trailing comma in each record.请注意,结果与您发布的结果略有不同:每条记录中没有尾随逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM