Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用XSLT压缩两个XML文件_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

用XSLT压缩两个XML文件

用XSLT压缩两个XML文件,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我必须创建XML源文件: 1.xml 约翰 雪 2.xml 琼尼 山丘 目标文件 约翰 雪 琼尼 山丘 我需要合并这两个文件以及更改元素名称。到目前为止,我的XSLT文件如下所示: XSLT文件 但是,输出文件中的间距错误,我无法添加附加元素并更改其他元素。请修改,如下所示 <xsl:template match="/"> <employee_list> <employee> <xsl:ap

我必须创建XML源文件:

1.xml

约翰
雪
2.xml

琼尼
山丘
目标文件

约翰
雪
琼尼
山丘
我需要合并这两个文件以及更改元素名称。到目前为止,我的XSLT文件如下所示:

XSLT文件

但是,输出文件中的间距错误,我无法添加附加元素并更改其他元素。

请修改
,如下所示

<xsl:template match="/">
    <employee_list>
        <employee>
            <xsl:apply-templates select="person/*" />
        </employee>
        <employee>
            <xsl:apply-templates select="document('2.xml')/person/*" />
        </employee>
    </employee_list>
</xsl:template>

添加另外两个模板以修改元素名称,即<代码>至


完整的XSLT和输出如下所示

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

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

    <!-- Prepare output structure -->
    <xsl:template match="/">
        <employee_list>
            <employee>
                <xsl:apply-templates select="person/*" />
            </employee>
            <employee>
                <xsl:apply-templates select="document('2.xml')/person/*" />
            </employee>
        </employee_list>
    </xsl:template>

    <!-- Rename <firstname> to <first> -->
    <xsl:template match="firstname">
        <first>
            <xsl:apply-templates />
        </first>
    </xsl:template>

    <!-- Rename <lastname> to <last> -->
    <xsl:template match="lastname">
        <last>
            <xsl:apply-templates />
        </last>
    </xsl:template>
</xsl:stylesheet>

输出

<employee_list>
    <employee>
        <first>John</first>
        <last>Snow</last>
    </employee>
    <employee>
        <first>Jonny</first>
        <last>Hill</last>
    </employee>
</employee_list>

约翰
雪
琼尼
山丘

这一款非常好用。然而,还有一个问题,如果我的目标文件中还有一个我不想在目标文件中包含的元素年龄,该怎么办。如何“取消选择”该元素?您可以在XSL中添加另一个模板以不处理
元素。这可以通过添加以下代码来完成。XSLT最初将应用
下的所有模板,处理
并忽略
元素,从而转换为预期的输出。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:copy>
                <xsl:apply-templates select="person"/>

                <xsl:apply-templates select="document('2.xml')/person"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
<xsl:template match="/">
    <employee_list>
        <employee>
            <xsl:apply-templates select="person/*" />
        </employee>
        <employee>
            <xsl:apply-templates select="document('2.xml')/person/*" />
        </employee>
    </employee_list>
</xsl:template>
<!-- Rename <firstname> to <first> -->
<xsl:template match="firstname">
    <first>
        <xsl:apply-templates />
    </first>
</xsl:template>

<!-- Rename <lastname> to <last> -->
<xsl:template match="lastname">
    <last>
        <xsl:apply-templates />
    </last>
</xsl:template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

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

    <!-- Prepare output structure -->
    <xsl:template match="/">
        <employee_list>
            <employee>
                <xsl:apply-templates select="person/*" />
            </employee>
            <employee>
                <xsl:apply-templates select="document('2.xml')/person/*" />
            </employee>
        </employee_list>
    </xsl:template>

    <!-- Rename <firstname> to <first> -->
    <xsl:template match="firstname">
        <first>
            <xsl:apply-templates />
        </first>
    </xsl:template>

    <!-- Rename <lastname> to <last> -->
    <xsl:template match="lastname">
        <last>
            <xsl:apply-templates />
        </last>
    </xsl:template>
</xsl:stylesheet>
<employee_list>
    <employee>
        <first>John</first>
        <last>Snow</last>
    </employee>
    <employee>
        <first>Jonny</first>
        <last>Hill</last>
    </employee>
</employee_list>