我对 xsl 转换有疑问。我没有得到我期望的结果,我看不出有什么问题。我认为问题与命名空间有关。你能帮助我吗?
这是我的服务将收到的 xml。我想将多值元素分离到一些新节点。
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap-env:Header/>
<soap-env:Body>
<testService facade="Test" xmlns="http://new.webservice.namespace">
<input>
<Data1>Data 1</Data1>
<Data2>Data 2</Data2>
<ParamResponses>
<ParamResponse>
<Name>DATAONE</Name>
<ValParam>Text 1</ValParam>
</ParamResponse>
<ParamResponse>
<Name>DATATWO</Name>
<ValParam>Text 2</ValParam>
</ParamResponse>
<ParamResponse>
<Name>MULTIVALUED</Name>
<ValParam>001</ValParam>
<ValParam>002</ValParam>
</ParamResponse>
<ParamResponse>
<Name>DATATHREE</Name>
<ValParam>Text 3</ValParam>
</ParamResponse>
</ParamResponses>
</input>
</testService>
</soap-env:Body>
</soap-env:Envelope>
这是我正在申请的 xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@facade">
<xsl:attribute name="facade">
<xsl:text>FacadeReplaced</xsl:text>
</xsl:attribute>
</xsl:template>
<xsl:template match="input/ParamResponses">
<ParamResponses>
<xsl:for-each select="ParamResponse[Name!='MULTIVALUED']">
<ParamResponse>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</ParamResponse>
</xsl:for-each>
</ParamResponses>
<MultiValueParamResponses>
<MultiValueParamResponse>
<Name>MULTIVALUED</Name>
<xsl:variable name="items" select="//input/ParamResponses/ParamResponse[Name='MULTIVALUED']/ValParam"/>
<ValueList>
<xsl:for-each select="$items">
<value>
<xsl:value-of select="."/>
</value>
</xsl:for-each>
</ValueList>
</MultiValueParamResponse>
</MultiValueParamResponses>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这是我得到的:
<?xml version="1.0" encoding="UTF-16"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap-env:Header/>
<soap-env:Body>
<testService facade="FacadeReplaced" xmlns="http://new.webservice.namespace">
<input>
<Data1>Data 1</Data1>
<Data2>Data 2</Data2>
<ParamResponses>
<ParamResponse>
<Name>DATAONE</Name>
<ValParam>Text 1</ValParam>
</ParamResponse>
<ParamResponse>
<Name>DATATWO</Name>
<ValParam>Text 2</ValParam>
</ParamResponse>
<ParamResponse>
<Name>MULTIVALUED</Name>
<ValParam>001</ValParam>
<ValParam>002</ValParam>
</ParamResponse>
<ParamResponse>
<Name>DATATHREE</Name>
<ValParam>Text 3</ValParam>
</ParamResponse>
</ParamResponses>
</input>
</testService>
</soap-env:Body>
</soap-env:Envelope>
但这就是我想要获得的:
<?xml version="1.0" encoding="UTF-16"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap-env:Header/>
<soap-env:Body>
<testService facade="FacadeReplaced" xmlns="http://new.webservice.namespace">
<input>
<Data1>Data 1</Data1>
<Data2>Data 2</Data2>
<ParamResponses>
<ParamResponse>
<ParamResponse>
<Name>DATAONE</Name>
<ValParam>Text 1</ValParam>
</ParamResponse>
</ParamResponse>
<ParamResponse>
<ParamResponse>
<Name>DATATWO</Name>
<ValParam>Text 2</ValParam>
</ParamResponse>
</ParamResponse>
<ParamResponse>
<ParamResponse>
<Name>DATATHREE</Name>
<ValParam>Text 3</ValParam>
</ParamResponse>
</ParamResponse>
</ParamResponses>
<MultiValueParamResponses>
<MultiValueParamResponse>
<Name>MULTIVALUED</Name>
<ValueList>
<value>001</value>
<value>002</value>
</ValueList>
</MultiValueParamResponse>
</MultiValueParamResponses>
</input>
</testService>
</soap-env:Body>
</soap-env:Envelope>
如果我将 xmlns 添加到元素输入,我已经正确获得它,但这不是我将通过 web 服务收到的。
转换应该是 XSLT 1.0。