xslt如何摆脱额外的不需要的OAuth标签

时间:2017-07-19 07:50:26

标签: xml xslt

我正在尝试转换以下xml:

<OAuth>
  <audience>aabd69c9-6d97-4fdf-8bd5-80d0c8fb1ed4</audience>
  <user_id>ABELG</user_id>
  <scope>resource.WRITE resource.READ</scope>
  <expires_in>3574</expires_in>
  <return_code>200</return_code>
  <return_message>Success</return_message>
</OAuth>

进入下面:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ng3="https://myurl.com/OAuth">
  <soap:Body>
    <ng3:OAuth>
     <ng3:client_id>aabd69c9-6d97-4fdf-8bd5-80d0c8fb1ed4</ng3:client_id>
     <ng3:Username>ABELG</ng3:Username>
     <ng3:scope>resource.WRITE resource.READ</ng3:scope>
     <ng3:expires_in>3574</ng3:expires_in>
     <ng3:return_code>200</ng3:return_code>
     <ng3:return_message>Success</ng3:return_message>
    </ng3:OAuth>
  </soap:Body>
</soap:Envelope>

使用:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
            xmlns:ng3="https://myurl.com/OAuth">
<xsl:output method="xml" />

<xsl:template match="OAuth">
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
          <xsl:element name="ng3:OAuth">
    <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
          </xsl:element>
    </soap:Body>
    </soap:Envelope>
</xsl:template>

<xsl:template match="audience">
   <xsl:element name="ng3:client_id">
    <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

<xsl:template match="user_id">
   <xsl:element name="ng3:Username">
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

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

但是,在进行转换后,我得到了一个额外的不需要的OAuth标记(没有名称空间前缀的标记):

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ng3="https://myurl.com/OAuth">
  <soap:Body>
    <ng3:OAuth>
       <OAuth>
         <ng3:client_id>aabd69c9-6d97-4fdf-8bd5-80d0c8fb1ed4</ng3:client_id>
         <ng3:Username>ABELG</ng3:Username>
         <ng3:scope>resource.WRITE resource.READ</ng3:scope>
         <ng3:expires_in>3574</ng3:expires_in>
         <ng3:return_code>200</ng3:return_code>
         <ng3:return_message>Success</ng3:return_message>
       </OAuth>
    </ng3:OAuth>
  </soap:Body>
</soap:Envelope>

当我设法摆脱额外的OAuth标签时,我丢失了封闭的肥皂标签。请帮助这个XSLT新手。

1 个答案:

答案 0 :(得分:1)

从模板中删除@元素

xsl:copy