arrays - 使用 XSLT 将 XML 元素移动到不同的节点
问题描述
下面是 XML 输入负载。我正在寻找一个 xml 输出应该在每个地址节点内都有“类型”元素。下面是传入的请求 XML
<rsp:response xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1"
xmlns:resp="resp.com/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:响应>
这是使用的 XSLT,它没有提供所需的输出。使用此 XSLT,所有“类型”元素都反映在每个地址块中。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1"
xmlns:resp="resp.com/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:样式表>
所需的输出 XML
<rsp:response
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1"
xmlns:resp="resp.com/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:响应>
解决方案
试试这种方式:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:resp="resp.com/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>
重新尝试:
永远不需要使用像
*[local-name()='type']
;你应该找出是什么
//
意思。
推荐阅读
- java - 错误:未加载 mongodb.jdbc.MongoDriver。你确定你在 :jdbc_driver_library 中包含了正确的 jdbc 驱动程序吗?
- java - 当用户尝试打开任何工作(.doc)文件时,如何以编程方式打开 xyz 程序?
- reactjs - 单击它们时如何使3个按钮显示和隐藏内容?
- vue.js - 如何列出 vue 组件中所有可用的插槽
- apache-flink - Flink Streaming API:如何将元素发送到多个(但不是全部)分区?
- ats - 你如何对负 1 进行模式匹配?
- flutter - Google Assistant 和 Siri 的 Flutter 集成 - 语音交互
- mercurial - 将一个 .hgignore 文件包含在另一个文件中的方法
- javascript - React Native - 使用 AsyncStorage 将数据存储在另一个屏幕中
- javascript - 在 Android 上离线运行 Service Worker 上的任务