我有一个场景,我需要使用自定义命名空间前缀转换XML,但不想在结果XML中包含该命名空间。
原始XML:
<?xml version="1.0" encoding="UTF-8"?>
<order>
<subject>
<field name="firstname" id="10"><![CDATA[]]></field>
<field name="middlename" id="27"/>
<field name="lastname" id="13"><![CDATA[Singh]]></field>
<field name="email" id="9"><![CDATA[]]></field>
<field name="gender2" id="12"/>
</subject>
</order>
XSLT:
<xsl:stylesheet version="2.0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cubiksonline.com/CubiksOnline/WebServices/201207" xmlns:cub="http://schemas.datacontract.org/2004/07/Cubiks.CubiksOnline.WebServices.DataContracts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xsl xs fn cub soapenv ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<cub:ParticipantToRegister>
<xsl:variable name="firstname" select="//*[local-name()='subject']/*[local-name()='field'][@*[local-name()='name']='firstname']"/>
<xsl:variable name="lastname" select="//*[local-name()='subject']/*[local-name()='field'][@*[local-name()='name']='lastname']"/>
<xsl:variable name="email" select="//*[local-name()='subject']/*[local-name()='field'][@*[local-name()='name']='email']"/>
<cub:FirstName>
<xsl:value-of select="$firstname"/>
</cub:FirstName>
<cub:LastName>
<xsl:value-of select="$lastname"/>
</cub:LastName>
<cub:EmailAddress>
<xsl:value-of select="$email"/>
</cub:EmailAddress>
<cub:IsAnonymous>
<xsl:choose>
<xsl:when test="(($firstname != '') and ($email != ''))">
<xsl:text>false</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>true</xsl:text>
</xsl:otherwise>
</xsl:choose>
</cub:IsAnonymous>
<cub:Gender>
<xsl:value-of select="//*[local-name()='subject']/*[local-name()='field'][@*[local-name()='name']='gender2']"/>
</cub:Gender>
<cub:ExternalUserID>HRNX_Test</cub:ExternalUserID>
</cub:ParticipantToRegister>
</xsl:template>
</xsl:stylesheet>
输出:
<cub:ParticipantToRegister xmlns:cub="http://schemas.datacontract.org/2004/07/Cubiks.CubiksOnline.WebServices.DataContracts">
<cub:FirstName />
<cub:LastName>Singh</cub:LastName>
<cub:EmailAddress />
<cub:IsAnonymous>true</cub:IsAnonymous>
<cub:Gender />
<cub:ExternalUserID>HRNX_Test</cub:ExternalUserID>
</cub:ParticipantToRegister>
预期产出:
<cub:ParticipantToRegister>
<cub:FirstName />
<cub:LastName>Singh</cub:LastName>
<cub:EmailAddress />
<cub:IsAnonymous>true</cub:IsAnonymous>
<cub:Gender />
<cub:ExternalUserID>HRNX_Test</cub:ExternalUserID>
</cub:ParticipantToRegister>
请让我知道这是否可行,如果是的话可能是同样的解决方案。