XSLT更改名称空间

时间:2015-05-27 11:46:27

标签: xml xslt

我有这个XML代码:

    <soapenv:Envelope xmlns:soapenv="something2" xmlns:oas="something1" xmlns:bill="something">
    <soapenv:Header>
    <wsse:Security xmlns:wsse="something_else">
     <wsse:UsernameToken>
        <wsse:Username>SYSUSER</wsse:Username>
        <wsse:Password Type="PasswordText">sysuser00</wsse:Password>
     </wsse:UsernameToken>
    </wsse:Security>
   </soapenv:Header>
    <soapenv:Body>
   <GetBillList xmlns="old_uri" xmlns:xsi="else" xsi:schemaLocation="old_schema">
   <Case>
   <CaseID>699677</CaseID> 
   <BillGroup casref="123">
   <BillGroupID>1</BillGroupID> 
   </BillGroup>
   </Case>
   <StartDate>2014-06-12</StartDate> 
   </GetBillList>
   </soapenv:Body>
   </soapenv:Envelope>

我想更改old_uri&amp; old_schema进入new_uri&amp;分别为new_schema,以及属性转换为BillGroup节点的元素。

使用XSLT:

   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bill="something">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="@*|node()">
   <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
   </xsl:template>
   <xsl:template match="bill:BillGroup">
   <xsl:element name="{local-name()}">
   <xsl:value-of select="."/>
   </xsl:element>
   </xsl:template>
   </xsl:stylesheet>

目标XML:

   <soapenv:Envelope xmlns:soapenv="something2" xmlns:oas="something1" xmlns:bill="something">
    <soapenv:Header>
    <wsse:Security xmlns:wsse="something_else">
        <wsse:UsernameToken>
            <wsse:Username>SYSUSER</wsse:Username>
            <wsse:Password Type="PasswordText">sysuser00</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
   </soapenv:Header>
    <soapenv:Body>
    <GetBillList xmlns="new_uri" xmlns:xsi="else" xsi:schemaLocation="new_schema">
        <Case>
            <CaseID>699677</CaseID>
            <BillGroup>
                <casref>123</casref>
                <BillGroupID>1</BillGroupID>
            </BillGroup>
        </Case>
        <StartDate>2014-06-12</StartDate>
       </GetBillList>
        </soapenv:Body>
     </soapenv:Envelope>

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在输入XML中,billGroup元素是old_uri命名空间的一部分,而不是something命名空间。这意味着您需要相应地调整名称空间前缀。如果要将属性更改为元素,还需要将其更改为与billGroup的属性匹配:

<xsl:template match="old:BillGroup/@*">
   <xsl:element name="{local-name()}" namespace="new_uri">
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

此处old是绑定到uri old_uri的前缀。

要将命名空间uri从old_uri更改为new_uri,您可以拥有一个与旧命名空间中的元素匹配的模板

<xsl:template match="old:*">
   <xsl:element name="{local-name()}" namespace="new_uri">
      <xsl:apply-templates select="@*|node()"/>
   </xsl:element>
</xsl:template>

要更改架构位置,请使用与属性匹配的模板

<xsl:template match="@xsi:schemaLocation">
   <xsl:attribute name="{name()}">
      <xsl:text>new_schema</xsl:text>
   </xsl:attribute>
</xsl:template>

试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="else" xmlns:old="old_uri" >
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="@xsi:schemaLocation">
  <xsl:attribute name="{name()}">
    <xsl:text>new_schema</xsl:text>
  </xsl:attribute>
</xsl:template>

<xsl:template match="old:*">
  <xsl:element name="{local-name()}" namespace="new_uri">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="old:BillGroup/@*">
  <xsl:element name="{local-name()}" namespace="new_uri">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>