简体   繁体   English

使用xslt将xml复杂节点元素拆分为多个节点

[英]Split xml complex node elements into multiple nodes using xslt

Is there a way to split a parent node into multiple nodes using xslt? 有没有一种方法可以使用xslt将父节点拆分为多个节点?

I want to transform the source xml into destination. 我想将源xml转换为目标。

Source xml 源xml

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
        <channel>
            <item>
                <id>Some id</id>
                <node1>
                    <node2 size="10.5" code="abcd"></node2>
                    <node2 size="10" code="cdef"></node2>        
                </node1>
            </item>
        </channel>
    </rss>

Destination xml 目标xml

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
        <channel>
            <item>
                <id>Some id_10.5</id>
                <size>10.5</size>
                <code>abcd</code>
            </item>
            <item>
                <id>Some id_10</id>
                <size>10</size>
                <code>cdef</code>
            </item>
        </channel>
    </rss>

If you notice the value of node id in destination xml, it has an underscore and size appended to it. 如果您在目标xml中注意到节点ID的值,则该值后面会附加一个下划线和大小。

Very straightforward, it's not clear why you are finding it difficult. 非常简单,不清楚为什么会遇到困难。

<xsl:template match="node2">
            <item>
                <id><xsl:value-of select="../../id"/>_<xsl:value-of select="@size"/></id>
                <size><xsl:value-of select="@size"/></size>
                <code><xsl:value-of select="@code"/></code>
            </item> 
</xsl:template> 

plus some trivial template rules for other elements. 加上一些其他元素的简单模板规则。

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

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