我正在尝试使用WSO2 ESB模板中的XSLT重命名标签。
我所有的尝试都会给我带来相同的错误:无法使用XSLT结果创建煎蛋。
我尝试过的一种变体是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:param name="response_tag"/>
<xsl:template match="RESPONSE">
<xsl:element name="{$response_tag}" >
<xsl:for-each select="/RESPONSE/*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
另一个是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="request_tag"/>
<xsl:template match="RESPONSE">
<xsl:element name="{$request_tag}">
<xsl:apply-templates select="node() | @*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我在做什么错?
ps。参数response_tag
值类似于rns:NameOfResponse
名称空间(rns
)和NameOfResponse
每次都可能不同。名称空间在XML的信封标签内部,因此末尾(如果转换起作用),XML将是有效的。
输入将是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rns="http://www.example.com/example">
<soapenv:Header/>
<soapenv:Body>
<RESPONSE xmlns="http://ws.apache.org/ns/synapse">
<tag>123</tag>
<another_tag>20160622134457473</another_tag>
...
</RESPONSE>
</soapenv:Body>
</soapenv:Envelope>
结果应为:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rns="http://www.example.com/example">
<soapenv:Header/>
<soapenv:Body>
<rns:NameOfResponse>
<tag>123</tag>
<another_tag>20160622134457473</another_tag>
...
</rns:NameOfResponse>
</soapenv:Body>
</soapenv:Envelope>
pps。尝试从响应标签中删除名称空间 - 即响应标签看起来像:NameOfTag
现在。相同的错误。
我猜你想做这样的事情:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:syn="http://ws.apache.org/ns/synapse"
xmlns="http://www.example.com/example"
exclude-result-prefixes="syn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="response_tag"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="syn:RESPONSE">
<xsl:element name="{substring-after($response_tag, ':')}" >
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="syn:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,使用参数response_tag='rns:NameOfResponse'
,结果为:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<NameOfResponse xmlns="http://www.example.com/example">
<tag>123</tag>
<another_tag>20160622134457473</another_tag>
...
</NameOfResponse>
</soapenv:Body>
</soapenv:Envelope>
请注意,NameOfResponse
元素声明a 默认名称空间 - ,这是其后代与输入相同的方式继承的。
添加:
要获得问题中显示的确切输出,您可以做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:syn="http://ws.apache.org/ns/synapse"
exclude-result-prefixes="syn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="response_tag"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="syn:RESPONSE">
<xsl:element name="{$response_tag}" namespace="http://www.example.com/example">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="syn:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
警告:我对WSO2一无所知;这就是它应该在XSLT中工作的方式。
发送此有效载荷:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<RESPONSE>
<tag>123</tag>
<another_tag>20160622134457473</another_tag>
</RESPONSE>
</soapenv:Body>
</soapenv:Envelope>
这个代理:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="TestSOF" transports="http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
<property name="RequestTagNamespace" value="http://wso2.com"/>
<property name="RequestTagName" value="NameOfResponse"/>
<xslt key="SOFXSL">
<property name="request_tag_namespace" expression="get-property('RequestTagNamespace')"/>
<property name="request_tag" expression="get-property('RequestTagName')"/>
</xslt>
<log level="full"/>
</inSequence>
</target>
<description/>
</proxy>
在本地条目中使用此XSL样式表:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="SOFXSL">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="request_tag_namespace"/>
<xsl:param name="request_tag"/>
<xsl:template match="RESPONSE">
<xsl:element name="myns:{$request_tag}" namespace="{$request_tag_namespace}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="@*|*|comment()">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()|comment()|processing-instruction()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<description/>
</localEntry>
您将获得此结果:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<myns:tagname xmlns:myns="http://tagnamespace">
<tag>123</tag>
<another_tag>20160622134457473</another_tag>
</myns:tagname>
</soapenv:Body>
</soapenv:Envelope>