首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用XSLT删除wsse:安全标记?

如何使用XSLT删除wsse:安全标记?
EN

Stack Overflow用户
提问于 2022-05-13 10:19:05
回答 1查看 46关注 0票数 1

下面是我想要删除的两行xml,即“wsu:Created”和“wsse:Nonce”:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
         <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
            <wsu:Created
                    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2022-05-03T16:05:18.952Z
            </wsu:Created>
            <wsse:Nonce>+2Z+Xw8qQSBhES2ESxZoPQ==</wsse:Nonce>
         </wsse:UsernameToken>
      </wsse:Security>
      <ns:version xmlns:ns="http://example.com/m2m xmlns:ns0=http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">2.0.0</ns:version>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>

我尝试使用以下XSLT (首先删除当前),但它无法工作:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2m="http://example.com.com/m2m" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
   <xsl:output omit-xml-declaration="yes" />
   <!-- Complete copy of the request -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="Envelope/Header/Security/UsernameToken/Nonce"/>
</xsl:stylesheet>

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2m="http://example.com.com/m2m" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
   <xsl:output omit-xml-declaration="yes" />
   <!-- Complete copy of the request -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="wsse:Nonce"/>
</xsl:stylesheet>

感谢熟悉XSLT的人在这方面提供帮助,非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-13 11:09:27

您必须声明名称空间,并按如下方式使用它们:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:m2m="http://example.com.com/m2m" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output omit-xml-declaration="yes"  indent="yes"/>
  <!-- Complete copy of the request -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="wsse:Nonce|wsu:Created"/>
</xsl:stylesheet>

将上面的xslt应用于此源xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
      <wsse:UsernameToken>
        <wsse:Username>test</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
        <wsu:Created
          xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2022-05-03T16:05:18.952Z
        </wsu:Created>
        <wsse:Nonce>+2Z+Xw8qQSBhES2ESxZoPQ==</wsse:Nonce>
      </wsse:UsernameToken>
    </wsse:Security>
    <ns:version xmlns:ns="http://example.com/m2m xmlns:ns0=http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">2.0.0</ns:version>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body/>
</SOAP-ENV:Envelope>

将提供这一预期结果:

代码语言:javascript
复制
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                     SOAP-ENV:mustUnderstand="1">
         <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
      <ns:version xmlns:ns="http://example.com/m2m xmlns:ns0=http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">2.0.0</ns:version>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body/>
</SOAP-ENV:Envelope>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72227969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档