Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SOAP消息中使用XSLT替换命名空间uri_Xslt_Xslt 1.0_Exslt_Xslt Grouping - Fatal编程技术网

在SOAP消息中使用XSLT替换命名空间uri

在SOAP消息中使用XSLT替换命名空间uri,xslt,xslt-1.0,exslt,xslt-grouping,Xslt,Xslt 1.0,Exslt,Xslt Grouping,我有一条输入soap消息,试图用不同的字符串替换命名空间uri的一部分。我可以用不同的URI替换整个URI,但不能修改现有的URI。我需要查找“OLDSTRING”并替换为“NEWSTRING”。字符串VARIABLESTRING在每个输入xml中都不同,因此我应该保持输出xml中的原样 输入XML: 睾丸 输出XML: 睾丸 我已经尝试了以下XSL,并且能够更改名称空间URI,但是我只想将“OLDSTRING”替换为“NEWSTRING”,并保持其余字符串不变 XSLT: 这是否符合

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

输入XML:


睾丸
输出XML:


睾丸
我已经尝试了以下XSL,并且能够更改名称空间URI,但是我只想将“OLDSTRING”替换为“NEWSTRING”,并保持其余字符串不变

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="{replace(namespace-uri(), 'OLDSTRING', 'NEWSTRING')}" >
            <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>
<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>

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

<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>
<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>

睾丸
生成所需的正确结果

<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>
<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>

睾丸
No,XPath 1.0/XSLT 1.0中没有
replace()
函数。在发布代码之前测试代码是个好主意。