使用XSLT合并具有公共字段的两个XML

时间:2019-04-24 20:51:27

标签: xslt xslt-1.0 xslt-2.0 xslt-grouping

我有两个xml

  1. a.xml
  2. b.xml

下面是a.xml文件

<ConsumerKeyperUsers>

    <CONSUMERKEY_1>
        <CONSUMERKEY>QoChiX5FlNyqe1FuqU7VB2M</CONSUMERKEY>
    </CONSUMERKEY_1>
    <CONSUMERKEY_2>
        <CONSUMERKEY>uzGjRVxnxmP00zrnZPtB9sM</CONSUMERKEY>
    </CONSUMERKEY_2>
    <CONSUMERKEY_3>
        <CONSUMERKEY>RD37oO2jg4rlerxW5p6f76</CONSUMERKEY>
    </CONSUMERKEY_3>

</ConsumerKeyperUsers>

下面是b.xml

<Details>

<Detail>
<ConsumeyKey>QoChiX5FlNyqe1FuqU7VB2M</ConsumeyKey>
<Place>Ocean</Place>
<City>Urban</City>
</Detail>

<Detail>
<ConsumeyKey>uzGjRVxnxmP00zrnZPtB9sM</ConsumeyKey>
<Place>Road</Place>
<City>Rural</City>
</Detail>

<Detail>
<ConsumeyKey>RD37oO2jg4rlerxW5p6f76</ConsumeyKey>
<Place>Plane</Place>
<City>Semiurban</City>
</Detail>

<Detail>
<ConsumeyKey>likujyhtasasa</ConsumeyKey>
<Place>Ship</Place>
<City>Semirural</City>
</Detail>

</Details>

现在,我想获得类似于下面的输出

    <ConsumerKeyperUsers>

    <CONSUMERKEY_1>
    <CONSUMERKEY>QoChiX5FlNyqe1FuqU7VB2M</CONSUMERKEY>
    <Place>Ocean</Place>
    <City>Urban</City>
    </CONSUMERKEY_1>

    <CONSUMERKEY_2>
    <CONSUMERKEY>uzGjRVxnxmP00zrnZPtB9sM</CONSUMERKEY>
    <Place>Road</Place>
    <City>Rural</City>
    </CONSUMERKEY_2>

    <CONSUMERKEY_3>
    <CONSUMERKEY>RD37oO2jg4rlerxW5p6f76</CONSUMERKEY>
    <Place>Plane</Place>
    <City>Semiurban</City>
    </CONSUMERKEY_3>

    </ConsumerKeyperUsers>

我对xslt相当陌生。任何xslt版本都可以使用。

此外,我还想知道如何将这两个xml馈送到xslt。我们是否必须将其存储在xslt可以访问这些文件的服务器中。

任何有关这方面的指导都会很有帮助。

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用以下XSLT-1.0样式表实现目标:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" />
    <xsl:variable name="bDoc" select="document('b.xml')/Details" />
    <xsl:strip-space elements="*" />

    <xsl:template match="/ConsumerKeyperUsers">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[starts-with(local-name(),'CONSUMERKEY_')]">
        <xsl:variable name="keyValue" select="CONSUMERKEY/text()" />
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="$bDoc/Detail[ConsumeyKey/text()=$keyValue]"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

其输出为:

<?xml version="1.0"?>
<ConsumerKeyperUsers>
    <CONSUMERKEY_1>
        <Detail>
            <ConsumeyKey>QoChiX5FlNyqe1FuqU7VB2M</ConsumeyKey>
            <Place>Ocean</Place>
            <City>Urban</City>
        </Detail>
    </CONSUMERKEY_1>
    <CONSUMERKEY_2>
        <Detail>
            <ConsumeyKey>uzGjRVxnxmP00zrnZPtB9sM</ConsumeyKey>
            <Place>Road</Place>
            <City>Rural</City>
        </Detail>
    </CONSUMERKEY_2>
    <CONSUMERKEY_3>
        <Detail>
            <ConsumeyKey>RD37oO2jg4rlerxW5p6f76</ConsumeyKey>
            <Place>Plane</Place>
            <City>Semiurban</City>
        </Detail>
    </CONSUMERKEY_3>
</ConsumerKeyperUsers>

但是,您的命名方案并不是最佳选择,因为在CONSUMERKEY_...元素的末尾添加数字是不好的做法。您应该改用CONSUMERKEY作为元素的名称。这还将简化处理该XML所需的模板。

答案 1 :(得分:0)

  

我还想知道如何将这两个xml馈送到xslt。

告诉您的应用程序处理a.xml文件,并将路径作为参数传递到b.xml

使用keyb.xml查找数据-例如(未测试):

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="file2" select="'b.xml'" />
<xsl:key name="details" match="Detail" use="ConsumeyKey" />

<xsl:template match="/ConsumerKeyperUsers">
    <xsl:copy>
        <xsl:for-each select="*">
            <xsl:copy>
                <xsl:copy-of select="CONSUMERKEY"/>
                <xsl:variable name="details" select="key('details', CONSUMERKEY, document($file2))" />
                <xsl:copy-of select="$details/Place"/>
                <xsl:copy-of select="$details/City"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

使用XSLT 1.0处理器执行相同的操作稍微复杂一些:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="file2" select="'b.xml'" />
<xsl:key name="details" match="Detail" use="ConsumeyKey" />

<xsl:template match="/ConsumerKeyperUsers">
    <xsl:copy>
        <xsl:for-each select="*">
            <xsl:copy>
                <xsl:variable name="key" select="CONSUMERKEY" />
                <xsl:copy-of select="$key"/>
                <!--  switch context to file2 in order to use key -->
                <xsl:for-each select="document($file2)">
                    <xsl:variable name="details" select="key('details', $key)" />
                    <xsl:copy-of select="$details/Place"/>
                    <xsl:copy-of select="$details/City"/>
                </xsl:for-each> 
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>