使用 XSLT 将 XML 元素移动到不同的节点

编程入门 行业动态 更新时间:2024-10-28 14:32:54
本文介绍了使用 XSLT 将 XML 元素移动到不同的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

以下是 XML 输入负载.我正在寻找一个 xml 输出应该有类型"每个地址节点内的元素.下面是传入的请求 XML

Below is the XML Input payload. I'm looking for an xml output should have "type" element inside the each Address node. Below is the incoming request XML

<rsp:response xmlns:xs="http://www.w3/2001/XMLSchema"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:rsp="rsp/employee/Response/v30"
xmlns:res="res/Member/details/v1"
xmlns:resp="resp/details/v1">
        <res:employee>
            <resp:Employee>
                <resp:FirstName>abc</resp:FirstName>
                <resp:middleName></resp:middleName>
                <resp:details>
                    <resp:Details>
                        <resp:type>postal</resp:type>  
                        <resp:Addresses>
                            <resp:Address>
                                <resp:country>XYZ</resp:country>
                            </resp:Address>
                        </resp:Addresses>
                    </resp:Details>
                    <resp:Details>
                        <resp:type>ofc</resp:type> 
                        <resp:Addresses>
                            <resp:Address>
                                <resp:country>XYZ</resp:country>
                            </resp:Address>
                        </resp:Addresses>
                    </resp:Details>
                </resp:details>
            </resp:Employee>
        </res:employee>

</rsp:response>

</rsp:response>

这是使用的 XSLT,它没有提供所需的输出.使用此 XSLT 所有类型"元素反映在每个地址块中.

Here is the XSLT used and it is not giving desired output. Using this XSLT all "type" elements is reflecting in each address block.

<xsl:stylesheet xmlns:xsl="http://www.w3/1999/XSL/Transform"    
xmlns:xs="http://www.w3/2001/XMLSchema"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:rsp="rsp/employee/Response/v30"
xmlns:res="res/Member/details/v1"
xmlns:resp="resp/details/v1"
version="2.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>  
<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>  
<xsl:template match="//*[local-name()='response']/*[local-name()='employee']/*[local-name()='Employee']/*[local-name()='details']/*[local-name()='Details']/*[local-name()='Addresses']/*[local-name()='Address']">
    <xsl:copy>
        <xsl:apply-templates/>            
        <xsl:for-each select="//*[local-name()='response']/*[local-name()='employee']/*[local-name()='Employee']/*[local-name()='details']/*[local-name()='Details']/*[local-name()='type']">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name()='response']/*[local-name()='employee']/*[local-name()='Employee']/*[local-name()='details']/*[local-name()='Details']/*[local-name()='type']"/>

</xsl:stylesheet>

</xsl:stylesheet>

所需的输出 XML

<rsp:response
xmlns:xs="http://www.w3/2001/XMLSchema"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:rsp="rsp/employee/Response/v30"
xmlns:res="res/Member/details/v1"
xmlns:resp="resp/details/v1">
<res:employee>
    <resp:Employee>
        <resp:FirstName>abc</resp:FirstName>
        <resp:middleName/>
        <resp:details>
            <resp:Details>
                <resp:Addresses>
                    <resp:Address>
                        <resp:country>XYZ</resp:country>
                        <resp:type>postal</resp:type>
                    </resp:Address>
                </resp:Addresses>
            </resp:Details>
            <resp:Details>
                <resp:Addresses>
                    <resp:Address>
                        <resp:country>XYZ</resp:country>
                        <resp:type>ofc</resp:type>
                    </resp:Address>
                </resp:Addresses>
            </resp:Details>
        </resp:details>
    </resp:Employee>
</res:employee>

</rsp:response>

</rsp:response>

推荐答案

试试看:

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

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

<xsl:template match="resp:Address">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:copy-of select="../../resp:type"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="resp:type"/>

</xsl:stylesheet>


重新尝试:


Re your attempt:

永远不需要使用像 *[local-name()='type'];

您应该了解 // 的含义.

You should find out what // means.

这篇关于使用 XSLT 将 XML 元素移动到不同的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 01:12:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   元素   XSLT   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!