Xslt 在WSO2 ESB 4.0.3中删除子元素为空的XML元素
我在ESB中设置了一个直通代理web服务,它从DSS端点获取数据,类似于以下内容:Xslt 在WSO2 ESB 4.0.3中删除子元素为空的XML元素,xslt,wso2,wso2esb,Xslt,Wso2,Wso2esb,我在ESB中设置了一个直通代理web服务,它从DSS端点获取数据,类似于以下内容: <Products xmlns="http://wso2.host.com/Products"> <Product> <SKU>12345678</SKU> <ItemName xmlns="null">T Shirt</ItemName> <Restrictions>
<Products xmlns="http://wso2.host.com/Products">
<Product>
<SKU>12345678</SKU>
<ItemName xmlns="null">T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason>Reason A</Reason>
<Code>12</Code>
</Restriction>
</Restrictions>
</Product>
</Products>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Restrictions"/>
</xsl:stylesheet>
产品有可能不受限制,并将通过以下方式:
<Products xmlns="http://wso2.host.com/Products">
<Product>
<SKU>12345678</SKU>
<ItemName xmlns="null">T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason/>
<Code/>
</Restriction>
</Restrictions>
</Product>
</Products>
非常感谢您的帮助,我对WSO2不太熟悉,可能完全找不到正确的答案。提前谢谢
更新:我遵循@Jorge Infante Osorio的建议,添加了一个XSLT中介,它引用了一个本地条目,定义如下:
<Products xmlns="http://wso2.host.com/Products">
<Product>
<SKU>12345678</SKU>
<ItemName xmlns="null">T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason>Reason A</Reason>
<Code>12</Code>
</Restriction>
</Restrictions>
</Product>
</Products>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Restrictions"/>
</xsl:stylesheet>
这仍然不是;不起作用。但是,我可以通过添加3个XSLT中介并将它们指向此本地条目来删除它们:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(node())] | *[not(node()[2]) and node()/self::text() and not(normalize-space()) ] " />
</xsl:stylesheet>
我试图修改它以匹配像@Jorge Infante Osorio在其示例中所使用的限制模板,但我似乎无法将其正确无误。由于您的负载根据输出不断变化,因此建议您使用XSLT mediator,原因是您的序列将变小,可读性将提高。由于您的负载根据输出不断变化,因此建议您使用XSLT mediator,原因是您的序列将变小,可读性将提高。我的测试代理:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="testReplacement"
transports="http https"
startOnLoad="true"
statistics="enable"
trace="enable">
<description/>
<target>
<inSequence>
<log level="custom">
<property name="ACCESO A: "
value="Accediendo replacement TEST"/>
</log>
<payloadFactory media-type="xml">
<format>
<Products xmlns="">
<Product>
<SKU>12345678</SKU>
<ItemName>T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason></Reason>
<Code></Code>
</Restriction>
</Restrictions>
</Product>
</Products>
</format>
<args>
</args>
</payloadFactory>
<property name="razon" expression="//Products/Product/Restrictions/Restriction/Reason/text()"/>
<property name="longitudValue" expression="fn:string-length(get-property('razon'))"/>
<log level="custom">
<property name="BODY = " expression="$ctx:body"/>
<property name="razon" expression="$ctx:razon"/>
<property name="longitudValue" expression="$ctx:longitudValue"/>
</log>
<filter source="$ctx:longitudValue" regex="0.0">
<then>
<log level="custom">
<property name="RESULTADO" value="Esta vacio"/>
</log>
<xslt key="transformacionDeleteEmpty"/>
<log level="custom">
<property name="BODY = " expression="$ctx:body"/>
</log>
</then>
<else>
<log level="custom">
<property name="RESULTADO" value="No esta vacio"/>
</log>
</else>
</filter>
<respond/>
</inSequence>
<outSequence>
<log level="full"/>
<drop/>
</outSequence>
<faultSequence/>
</target>
</proxy>
以及XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="transformacionDeleteEmpty" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Restrictions"/>
</xsl:stylesheet>
</localEntry>
控制台中的输出:
[2018-04-06 11:21:53,210] INFO - LogMediator ACCESO A: = Accediendo replacement TEST
[2018-04-06 11:21:53,214] INFO - LogMediator BODY = = <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><Products><Product><SKU>12345678</SKU><ItemName>T Shirt</I
temName><Restrictions><Restriction><Reason/><Code/></Restriction></Restrictions></Product></Products></soapenv:Body>, razon = , longitudValue = 0.0
[2018-04-06 11:21:53,215] INFO - LogMediator RESULTADO = Esta vacio
[2018-04-06 11:21:53,249] INFO - LogMediator BODY = = <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><Products>
<Product>
<SKU>12345678</SKU>
<ItemName>T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason/>
<Code/>
</Restriction>
</Restrictions>
</Product>
</Products></soapenv:Body>
[2018-04-06 11:21:53,307] INFO - LogMediator BODY = = <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><Products>
<Product>
<SKU>12345678</SKU>
<ItemName>T Shirt</ItemName>
</Product>
</Products></soapenv:Body>
[2018-04-06 11:21:53210]信息-LogMediator ACCESO A:=Accediendo更换测试
[2018-04-06 11:21:53214]信息-LogMediator BODY==12345678 T恤
,razon=,longitudValue=0.0
[2018-04-06 11:21:53215]INFO-logmediatorresultado=Esta vacio
[2018-04-06 11:21:53249]INFO-logmediatorbody==
12345678
T恤衫
[2018-04-06 11:21:53307]INFO-LogMediator BODY==
12345678
T恤衫
我的测试代理:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="testReplacement"
transports="http https"
startOnLoad="true"
statistics="enable"
trace="enable">
<description/>
<target>
<inSequence>
<log level="custom">
<property name="ACCESO A: "
value="Accediendo replacement TEST"/>
</log>
<payloadFactory media-type="xml">
<format>
<Products xmlns="">
<Product>
<SKU>12345678</SKU>
<ItemName>T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason></Reason>
<Code></Code>
</Restriction>
</Restrictions>
</Product>
</Products>
</format>
<args>
</args>
</payloadFactory>
<property name="razon" expression="//Products/Product/Restrictions/Restriction/Reason/text()"/>
<property name="longitudValue" expression="fn:string-length(get-property('razon'))"/>
<log level="custom">
<property name="BODY = " expression="$ctx:body"/>
<property name="razon" expression="$ctx:razon"/>
<property name="longitudValue" expression="$ctx:longitudValue"/>
</log>
<filter source="$ctx:longitudValue" regex="0.0">
<then>
<log level="custom">
<property name="RESULTADO" value="Esta vacio"/>
</log>
<xslt key="transformacionDeleteEmpty"/>
<log level="custom">
<property name="BODY = " expression="$ctx:body"/>
</log>
</then>
<else>
<log level="custom">
<property name="RESULTADO" value="No esta vacio"/>
</log>
</else>
</filter>
<respond/>
</inSequence>
<outSequence>
<log level="full"/>
<drop/>
</outSequence>
<faultSequence/>
</target>
</proxy>
以及XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="transformacionDeleteEmpty" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Restrictions"/>
</xsl:stylesheet>
</localEntry>
控制台中的输出:
[2018-04-06 11:21:53,210] INFO - LogMediator ACCESO A: = Accediendo replacement TEST
[2018-04-06 11:21:53,214] INFO - LogMediator BODY = = <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><Products><Product><SKU>12345678</SKU><ItemName>T Shirt</I
temName><Restrictions><Restriction><Reason/><Code/></Restriction></Restrictions></Product></Products></soapenv:Body>, razon = , longitudValue = 0.0
[2018-04-06 11:21:53,215] INFO - LogMediator RESULTADO = Esta vacio
[2018-04-06 11:21:53,249] INFO - LogMediator BODY = = <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><Products>
<Product>
<SKU>12345678</SKU>
<ItemName>T Shirt</ItemName>
<Restrictions>
<Restriction>
<Reason/>
<Code/>
</Restriction>
</Restrictions>
</Product>
</Products></soapenv:Body>
[2018-04-06 11:21:53,307] INFO - LogMediator BODY = = <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><Products>
<Product>
<SKU>12345678</SKU>
<ItemName>T Shirt</ItemName>
</Product>
</Products></soapenv:Body>
[2018-04-06 11:21:53210]信息-LogMediator ACCESO A:=Accediendo更换测试
[2018-04-06 11:21:53214]信息-LogMediator BODY==12345678 T恤
,razon=,longitudValue=0.0
[2018-04-06 11:21:53215]INFO-logmediatorresultado=Esta vacio
[2018-04-06 11:21:53249]INFO-logmediatorbody==
12345678
T恤衫
[2018-04-06 11:21:53307]INFO-LogMediator BODY==
12345678
T恤衫
尝试使用以下XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://wso2.host.com/Products">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p:Restrictions[not(normalize-space())]"/>
</xsl:stylesheet>
注意,我已经考虑了默认名称空间
http://wso2.host.com/Products
尝试使用以下XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://wso2.host.com/Products">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p:Restrictions[not(normalize-space())]"/>
</xsl:stylesheet>
注意,我已经考虑了默认名称空间
http://wso2.host.com/Products
我尝试了这个方法,但最终结果没有删除元素。请查看我更新的问题描述。你确定吗?请检查我的代理,我使用与你相同的XML以及它从EI控制台获取的输出!!!可能是因为您使用的ESB4.0.3是一个非常旧的版本。我尝试了这个版本,但最终没有删除元素。请查看我更新的问题描述。你确定吗?请检查我的代理,我使用与你相同的XML以及它从EI控制台获取的输出!!!可能是因为您使用的ESB 4.0.3版本非常旧。请尝试将上一个模板中的匹配更改为match=“Restrictions[not(normalize-space())]”
。如果这不起作用,请确保限制
不在默认名称空间中。@DanielHaley您的第一个建议不起作用-但我注意到我在根元素上有一个名称空间,但我没有包含该名称空间。编辑了我的答案,包括这个-这就是影响模板的原因吗?是的,就是这样。见下面我的答案。希望能有帮助!我的回答有用吗?请尝试将上一个模板中的匹配更改为match=“Restrictions[not(normalize-space())]”
。如果这不起作用,请确保限制
不在默认名称空间中。@DanielHaley您的第一个建议不起作用-但我注意到我在根元素上有一个名称空间,但我没有包含该名称空间。编辑了我的答案,包括这个-这就是影响模板的原因吗?是的,就是这样。见下面我的答案。希望能有帮助!我的回答有用吗?这就行了!非常感谢。在XSLT之前,我还有一个丰富的中介,为了让它工作,我必须将它移到XSLT之后。这就成功了!非常感谢。在XSLT之前,我还有一个Enrich中介,必须将它移动到XSLT之后才能工作。