xslt - XSLT:如果结果为真,则匹配字段字符串,然后复制完整的相应节点
问题描述
我是 XSLT 的新手。
我需要帮助和指导来完成以下任务
我有一个输入 xml:
<Prices>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>test12345</Name>
<Type>Data Stored - Tables</Type>
<group>--</group>
<Cost>0.00</Cost>
</Price>
我需要在每个元素中搜索 group ,例如在这种情况下是shell。一旦外壳匹配,完整的节点应该出现在输出 xml 中,例如
<Prices>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
</Prices>
我尝试使用很多 xslt,但无法获得此输出。
有人可以帮帮我吗。
在这里,输入文件只有 3 个节点,但实际上它也可以有超过 50 个节点。
尝试使用这个 xslt 但它失败了
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Identity transform -->
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:param name="str" select="'shell'"/>
<xsl:template match="@* | node()">
<output>
<xsl:for-each select="//*[.=$str]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
感谢 Martin 帮助以前的用例。
现在我也希望这包括搜索两个或多个变量。为了理解,下面是xml输入文件
<Prices>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>test12345</Name>
<Type>Data Stored - Tables</Type>
<group>--</group>
<Cost>0.00</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell12345</group>
<Cost>0.54</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>nutshell1234</group>
<Cost>0.60</Cost>
</Price>
</Prices>
我希望输出是
<Prices>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell</group>
<Cost>0.23</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>shell12345</group>
<Cost>0.54</Cost>
</Price>
<Price>
<Name>1234</Name>
<Type>account</Type>
<group>nutshell1234</group>
<Cost>0.60</Cost>
</Price>
</Prices>
此外,具有 sheel、nutshell、shell12345 的组也可能是不同的,也没有匹配任何字符串。但好的一点是我必须搜索的内容是否可用,因此如果需要,我们也可以提供硬编码值。
解决方案
鉴于您从身份转换模板开始,我认为最简单的方法是为那些Price
没有所需group
值的元素添加一个空模板:
<xsl:template match="Price[not(group = $str)]"/>
因为这样这些就不会被复制。
在https://xsltfiddle.liberty-development.net/bdxtri的 XSLT 3 版本和在http://xsltransform.hikmatu.com/eiZQaET的 XSLT 2 版本在线。
推荐阅读
- java - 选择一个随机数来显示胜利或失败的导航游戏
- c# - 任务计划程序中的多个任务是否可以同时运行控制台应用程序(.exe 文件)?
- c# - WCF 在客户端调用服务
- c - 绑定失败:无法分配请求的地址
- powershell - 在 Powershell 中从 CSV 中删除标头
- excel - 使用 VBA 填充 Zillow 搜索框(Zillow 是基于反应的网站)
- c# - 所需的数据注释抛出错误而不是验证客户端
- c# - 将两个不同的 JSON 对象 FromBody 传递给控制器
- mongodb - 当我运行 docker compose 时,我的 golang(摄取)容器无法显示“建立 Mongo 会话时出错”
- spring-mvc - 在“java.util.LinkedHashMap”类型的对象上找不到 - 也许不是公开的?