在WSO2 ESB 4.0.3中删除带有空子项的XML元素

时间:2018-04-03 00:12:30

标签: xslt wso2 wso2esb

我在ESB中设置了一个传递代理Web服务,用于从DSS端点获取数据,类似于以下内容:

<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>

产品可能没有限制,并且可能会这样:

<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>

我想删除整个<Restrictions>元素,以便响应如下:

<Products xmlns="http://wso2.host.com/Products">
    <Product>
        <SKU>12345678</SKU>
        <ItemName xmlns="null">T Shirt</ItemName>
    </Product>
</Products>

我正在尝试在out序列中使用Enrich中介来替换它,但我不确定要用什么代替它,或者如果这是最好的方法。我有的Xpath表达式如下:

/Products/Product/Restrictions[string-length(Restriction/Reason[text()])=0]

任何帮助都非常受欢迎,我对WSO2并不是非常熟悉,可能完全错过了正确的答案。提前谢谢。

更新:我已经按照@Jorge Infante Osorio的建议添加了一个XSLT中介,它引用了一个定义如下的本地条目:

<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在他的例子中的Restrictions模板,但我似乎无法正确理解。

3 个答案:

答案 0 :(得分:1)

由于您的有效负载会根据输出不断变化,因此建议您使用XSLT介体,原因是您的序列尺寸较小且可读性会增加。

答案 1 :(得分:1)

我的测试代理:

<?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>

答案 2 :(得分:1)

尝试使用以下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