简体   繁体   English

XSLT嵌套到xml的平面结构

[英]XSLT nested to flat structure of xml

with xslt I would like to transform xml document from: 使用xslt我想从以下方式转换xml文档:

<element1>val1</element1>
<element2>
    <element3> value </element3>
    <element4> value </element4>
<element2>

to xml like: 到xml一样:

<element1>val1</element1>
<element3> value </element3>
<element4> value </element4>

I would like to delete <element2> , because it contain "children elements" and all other elements create as flat elements. 我想删除<element2> ,因为它包含“子元素”,所有其他元素都创建为平面元素。

Any idea? 任何想法?

The following stylesheet will remove all elements that have children (except the root element), returning a "flat" list of leaf nodes only: 以下样式表将删除所有具有子元素的元素(根元素除外),仅返回叶节点的“平面”列表:

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

<xsl:template match="*/*[*]">
    <xsl:apply-templates select="*"/>
</xsl:template>

</xsl:stylesheet>

When applied to the following test input : 当应用于以下测试输入时

<world>
    <Europe>
        <Germany>
            <Berlin>no</Berlin>
            <Munich>yes</Munich>
        </Germany>
        <France>
            <Paris>no</Paris>
        </France>
        <Italy>
            <Rome>no</Rome>
            <Venice>yes</Venice>
            <Milano>no</Milano>
        </Italy>
    </Europe>
    <Asia>
        <China>
            <Beijin>no</Beijin>
            <Shanghai>yes</Shanghai>
        </China>
        <India>
            <Mumbay>no</Mumbay>
        </India>
    </Asia>
    <America>
        <USA>
            <NewYork>
                <NewYork>no</NewYork>
                <Albany>yes</Albany>
            </NewYork>
            <California>
                <LosAngeles>no</LosAngeles>
                <SanFranciso>no</SanFranciso>                       
            </California>
        </USA>
        <Canada>
            <Vancouver>no</Vancouver>
            <Montreal>yes</Montreal>
        </Canada>
        <Mexico>
            <Tijuana>no</Tijuana>
        </Mexico>
    </America>
</world>

the result will be: 结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<world>
   <Berlin>no</Berlin>
   <Munich>yes</Munich>
   <Paris>no</Paris>
   <Rome>no</Rome>
   <Venice>yes</Venice>
   <Milano>no</Milano>
   <Beijin>no</Beijin>
   <Shanghai>yes</Shanghai>
   <Mumbay>no</Mumbay>
   <NewYork>no</NewYork>
   <Albany>yes</Albany>
   <LosAngeles>no</LosAngeles>
   <SanFranciso>no</SanFranciso>
   <Vancouver>no</Vancouver>
   <Montreal>yes</Montreal>
   <Tijuana>no</Tijuana>
</world>

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

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