XSLT去除SOAP信封,但留下的命名空间(XSLT to remove SOAP envelope

2019-07-30 15:34发布

我需要从SOAP消息中删除SOAP信封。 对于那些想使用XSLT,而不是Java。 这将是用于操作这种类型的XML的更妥善的解决办法。

对于如我有一个SOAP消息:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:tar="namespace" 
                  xmlns:tar1="namespace">
    <soapenv:Header/>
    <soapenv:Body>
        <tar:RegisterUser>
            <tar1:Source>?</tar1:Source>
            <tar1:Profile>
                <tar1:EmailAddress>?</tar1:EmailAddress>

            </tar1:Profile>
        </tar:RegisterUser>
    </soapenv:Body>
</soapenv:Envelope>

我希望我的输出是这样的:

<tar:RegisterUser xmlns:tar="namespace" xmlns:tar1="namespace">
    <tar1:Source>?</tar1:Source>
    <tar1:Profile>
        <tar1:EmailAddress>?</tar1:EmailAddress>

    </tar1:Profile>
</tar:RegisterUser>

有人可以给我提供关于如何做到这一点的一些想法?

Answer 1:

这摆脱了的soapenv:元素命名空间声明。

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
>
  <xsl:output indent="yes" />

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

  <xsl:template match="soapenv:*">
    <xsl:apply-templates select="@* | node()" />
  </xsl:template>
</xsl:stylesheet>

结果:

<tar:RegisterUser xmlns:tar="namespace">
  <tar1:Source xmlns:tar1="namespace">?</tar1:Source>
  <tar1:Profile xmlns:tar1="namespace">
    <tar1:EmailAddress>?</tar1:EmailAddress>
  </tar1:Profile>
</tar:RegisterUser>


Answer 2:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    version="1.0">

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:copy-of select="/soapenv:Envelope/soapenv:Body/*"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="utf-8"?>
<tar:RegisterUser xmlns:tar="namespace" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tar1="namespace">
    <tar1:Source>?</tar1:Source>
    <tar1:Profile>
        <tar1:EmailAddress>?</tar1:EmailAddress>
    </tar1:Profile>
</tar:RegisterUser>

不幸的是,我无法找到任何简单的方法来删除xmlns:soapenv属性。



文章来源: XSLT to remove SOAP envelope but leave namespaces
标签: xml xslt