gpt4 book ai didi

java - 调整序列化 DOM 中的 XML 命名空间声明样式

转载 作者:行者123 更新时间:2023-12-02 02:22:26 27 4
gpt4 key购买 nike

请考虑作为对 SOAP 调用的响应而生成的两个等效 xml 文档。

文件一:

<?xml version="1.0" encoding="utf-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<ns2:ServerVersionInfo xmlns:ns3="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:ns2="http://schemas.microsoft.com/exchange/services/2006/types" MajorVersion="15" MinorVersion="1" MajorBuildNumber="845" MinorBuildNumber="34"/>
</S:Header>
<S:Body>
<ns3:GetUserAvailabilityResponse xmlns:ns3="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:ns2="http://schemas.microsoft.com/exchange/services/2006/types">
<ns3:FreeBusyResponseArray>
<ns3:FreeBusyResponse>
<ns3:ResponseMessage ResponseClass="Success">
<ns3:ResponseCode>NoError</ns3:ResponseCode>
</ns3:ResponseMessage>
<ns3:FreeBusyView>
<ns2:FreeBusyViewType>MergedOnly</ns2:FreeBusyViewType>
<ns2:MergedFreeBusy>0000</ns2:MergedFreeBusy>
</ns3:FreeBusyView>
</ns3:FreeBusyResponse>
</ns3:FreeBusyResponseArray>
</ns3:GetUserAvailabilityResponse>
</S:Body>
</S:Envelope>

文件二:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="15" MinorVersion="1" MajorBuildNumber="845" MinorBuildNumber="34"/>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetUserAvailabilityResponse xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
<FreeBusyResponseArray>
<FreeBusyResponse>
<ResponseMessage ResponseClass="Success">
<ResponseCode>NoError</ResponseCode>
</ResponseMessage>
<FreeBusyView>
<FreeBusyViewType xmlns="http://schemas.microsoft.com/exchange/services/2006/types">MergedOnly</FreeBusyViewType>
<MergedFreeBusy xmlns="http://schemas.microsoft.com/exchange/services/2006/types">0000</MergedFreeBusy>
</FreeBusyView>
</FreeBusyResponse>
</FreeBusyResponseArray>
</GetUserAvailabilityResponse>
</s:Body>
</s:Envelope>

如果我错了,请纠正我,但除了 xml 命名空间声明样式之外,这些 DOM 在语义上看起来很相似。我想从文档一的样式到文档二的样式调整我的java应用程序的jax-ws输出。如有必要,我可以使用 javax.xml.transform.Transformer 重新处理 DOM。

最佳答案

您可以在 XSLT 中这样做:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
<xsl:variable name="bindings">
<ns prefix="s:" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
<ns prefix="h:" uri="http://schemas.microsoft.com/exchange/services/2006/types"/>
<ns prefix="" uri="http://schemas.microsoft.com/exchange/services/2006/messages"/>
</xsl:variable>

<xsl:template match="*">
<xsl:variable name="p" select="$bindings/ns[@uri=namespace-uri(current())]/@prefix"/>
<xsl:element name="{$p}{local-name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:transform>

现已测试(尽管未使用 xsltproc)。

它提供了“文档二”的输出,除了 FreeBusyViewType 和 MergedFreeBusy 使用命名空间前缀“h”而不是在默认命名空间中没有前缀。您需要进行一些进一步的调整才能改变这一点,因为我的代码生成一个文档,其中给定命名空间中的所有元素都具有相同的前缀。我不知道为什么你应该优先选择你的输出。 (事实上​​,说实话,我不知道这个小练习的意义是什么。为什么会有人关心?)

关于java - 调整序列化 DOM 中的 XML 命名空间声明样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48303146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com