在 SOAP 消息中使用 XSLT 替换命名空间 uri [英] replace namespace uri using XSLT in SOAP message

查看:24
本文介绍了在 SOAP 消息中使用 XSLT 替换命名空间 uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入soap消息,试图用不同的字符串替换部分命名空间uri.我可以用不同的 URI 替换整个 URI,但不能修改现有的 URI.我需要寻找OLDSTRING"并替换为NEWSTRING".字符串 VARIABLESTRING 在每个输入 xml 中都不同,所以我应该在输出 xml 中保持原样

i have an input soap message, trying to replace part of namespace uri with a different string. I am able to replace entire URI with a different URI, but not able to modify the existing URI. I need to look for 'OLDSTRING' and replace with 'NEWSTRING'. The string VARIABLESTRING varies in in every input xml, so I should keep as it is in the output xml

输入 XML:

Input XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:schemas-OLDSTRING-com:VARIABLESTRING">
       <soapenv:Header/>
       <soapenv:Body>
          <requestMessage xmlns="urn:schemas-OLDSTRING-com:VARIABLESTRING">
            <merchantID>TESTID</merchantID>   
          </requestMessage>
       </soapenv:Body>
    </soapenv:Envelope>

输出 XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:schemas-NEWSTRING-com:transaction-data-1.69">
   <soapenv:Header/>
   <soapenv:Body>
      <requestMessage xmlns="urn:schemas-NEWSTRING-com:VARIABLESTRING">
        <merchantID>TESTID</merchantID>   
      </requestMessage>
   </soapenv:Body>
</soapenv:Envelope>

我已经尝试了以下 XSL,并且能够更改命名空间 URI,但我只想将 'OLDSTRING' 替换为 'NEWSTRING' 并保持剩余字符串不变.

I have tried the following XSL, and able to change the namespace URI, but i want to replace only 'OLDSTRING' with 'NEWSTRING' and keep remaining string as it is.

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|*|text()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//*[namespace-uri()='urn:schemas-OLDSTRING-com:VARIABLESTRING']">
        <xsl:element name="{local-name()}" namespace="urn:schemas-NEWSTRING-com:VARIABLESTRING" >
            <xsl:apply-templates select="@*|*|text()" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

推荐答案

这种处理在 XSLT 2.0 中很容易,但在 XSLT 1.0 中很棘手:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pTarget" select="'OLDSTRING'"/>
 <xsl:param name="pRepl" select="'NEWSTRING'"/>

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

 <xsl:template match=
  "*[starts-with(namespace-uri(), 'urn:schemas')
   or
     namespace::*[starts-with(., 'urn:schemas')]
    ]">

  <xsl:variable name="vNS" select="namespace-uri()"/>

  <xsl:variable name="vNewNS">
    <xsl:choose>
      <xsl:when test="not(contains($vNS, $pTarget))">
        <xsl:value-of select="$vNS"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="substring-before($vNS, $pTarget)"/>
        <xsl:value-of select="$pRepl"/>
        <xsl:value-of select="substring-after($vNS, $pTarget)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:element name="{name()}" namespace="{$vNewNS}">
   <xsl:copy-of select=
     "namespace::*
        [not(. = $vNS
            or
             starts-with(., 'urn:schemas')
           and
             contains(., $pTarget)
             )
        ]"/>

   <xsl:for-each select=
     "namespace::*
        [starts-with(., 'urn:schemas')
       and
         contains(., $pTarget)
        ]">
     <xsl:variable name="vNewNSUri" select=
     "concat(substring-before(., $pTarget),
             $pRepl,
             substring-after(., $pTarget)
             )
     "/>

     <xsl:variable name="vPrefix" select="name()"/>

     <xsl:variable name="vPref">
       <xsl:if test="$vPrefix">
         <xsl:value-of select="concat($vPrefix, ':')"/>
       </xsl:if>
     </xsl:variable>

     <xsl:variable name="vrtfDoc">
      <xsl:element name="{$vPref}dummy"
                   namespace="{$vNewNSUri}"/>
     </xsl:variable>

     <xsl:copy-of select=
     "ext:node-set($vrtfDoc)/*/namespace::*[. = $vNewNSUri]"/>
   </xsl:for-each>

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

当此转换应用于提供的 XML 文档时:

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:schemas-OLDSTRING-com:VARIABLESTRING">
    <soapenv:Header/>
    <soapenv:Body>
        <requestMessage xmlns="urn:schemas-OLDSTRING-com:VARIABLESTRING">
            <merchantID>TESTID</merchantID>
        </requestMessage>
    </soapenv:Body>
</soapenv:Envelope>

产生想要的、正确的结果:

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:schemas-NEWSTRING-com:VARIABLESTRING">
  <soapenv:Header />
  <soapenv:Body>
    <requestMessage xmlns="urn:schemas-NEWSTRING-com:VARIABLESTRING">
      <merchantID>TESTID</merchantID>
    </requestMessage>
  </soapenv:Body>
</soapenv:Envelope>

这篇关于在 SOAP 消息中使用 XSLT 替换命名空间 uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆