xml - 使用 XSLT 将带有前缀的 Soapenv、header、bodz 添加到 XML 标记
问题描述
我有以下要求足以满足一个 XML 文件。
- 删除主节点
- 移除现有的命名空间
- 添加soapenv信封、标题和正文标签。
- 将带有命名空间的前缀添加到父节点和其他节点。
到目前为止,我已经成功满足了前两个要求,但不是最后两个。有人可以帮我满足以下要求吗?先感谢您。
下面是我的输入xml:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MainNode xmlns:ns0="http://test/system">
<MainResponse>
<Header>
<Value>12AB</Value>
<QTY>10000</QTY>
<DetailList>
<ActualValue>1</ActualValue>
<ActualValue>2</ActualValue>
<ActualValue>3</ActualValue>
</DetailList>
</Header>
</MainResponse>
</ns0:MainNode>
目标 XML 应该是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<soapenv:Header/>
<soapenv:Body>
<a123:MainResponse xmlns:a123="http://www.ibm.com/test/One/Type">
<b345:Header xmlns:b345="http://www.ibm.com/test/One/DeType">
<b345:Value>12AB</b345:Value>
<b345:QTY>10000</b345:QTY>
<b345:DetailList>
<b345:ActualValue>1</b345:ActualValue>
<b345:ActualValue>2</b345:ActualValue>
<b345:ActualValue>3</b345:ActualValue>
</b345:DetailList>
</b345:Header>
</a123:MainResponse>
</soapenv:Body>
</soapenv:Envelope>
尝试过 XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" />
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- Removing first node -->
<xsl:template match="/">
<xsl:apply-templates select="*/*" />
</xsl:template>
<!-- Removing namespace -->
<xsl:template match="*">
<xsl:element name="{local-name()}" >
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
解决方案
请尝试下面的 XSLT。
您需要对模板进行选择性匹配,并相应地添加命名空间和前缀。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://test/system" exclude-result-prefixes="ns0">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:template match="ns0:MainNode">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<soapenv:Header />
<soapenv:Body>
<xsl:apply-templates />
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<xsl:template match="MainResponse">
<xsl:element name="a123:{local-name()}" namespace="http://www.ibm.com/test/One/Type">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="Header | Header//*">
<xsl:element name="b345:{local-name()}" namespace="http://www.ibm.com/test/One/DeType">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
输出
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<soapenv:Header />
<soapenv:Body>
<a123:MainResponse xmlns:a123="http://www.ibm.com/test/One/Type">
<b345:Header xmlns:b345="http://www.ibm.com/test/One/DeType">
<b345:Value>12AB</b345:Value>
<b345:QTY>10000</b345:QTY>
<b345:DetailList>
<b345:ActualValue>1</b345:ActualValue>
<b345:ActualValue>2</b345:ActualValue>
<b345:ActualValue>3</b345:ActualValue>
</b345:DetailList>
</b345:Header>
</a123:MainResponse>
</soapenv:Body>
</soapenv:Envelope>
推荐阅读
- google-photos - 是否可以将 Google Drive 中的相册添加到 Google Photos?
- javascript - 检查 observable 是否完整
- c++ - 如果函数被称为 constexpr,则有条件地 static_assert
- javascript - 为什么 linter 不对 vue js 中的模板进行 linting?
- pandas - 如何在时间序列数据帧的月份之间进行自相关?
- mongodb - 如何将元素添加到嵌套数组
- c# - 实体框架 6 将 _id 添加到外部表中的列并且看不到为什么?
- sql-server - 经典 ASP 脚本首次运行不同于后续运行
- python - 如何将列表作为新项目加入列表字典 - python?
- c# - 如果数据库已经存在,DbContext.Database.EnsureCreatedAsync 会抛出异常