我有这个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>
非常感谢您的帮助。
答案 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>