【发布时间】:2018-04-12 05:08:29
【问题描述】:
我在 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 序列中使用丰富的中介来将其替换为空,但我不确定用什么来替换它,或者这是否真的是最好的方法。我的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 在他的示例中使用的限制模板,但我似乎无法正确。
【问题讨论】:
-
尝试将上一个模板中的匹配更改为
match="Restrictions[not(normalize-space())]"。如果这不起作用,请确保Restrictions不在默认命名空间中。 -
@DanielHaley 您的第一个建议不起作用 - 但我注意到我在根元素上确实有一个我没有包含的命名空间。编辑了我的答案以包括这一点 - 是什么影响了模板?
-
是的,就是这样做的。请看下面我的回答。希望对您有所帮助!
-
我的回答有帮助吗?