如何解决从 foreach 中排除指定元素
我希望在 for-each 期间使用来自导入的 JSON 数据的结果映射排除某些元素。 我怀疑我正在运行的测试在将 XML 作为源运行时可能有效,但在 JSON 作为源时无效。
JSON 数据:
<data>
{
"storage": {
"pencils": 12,"milk": 8,"rulers": 4
}
}
</data>
**
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:storage="http://www.example.com/1"
xmlns:office="http://www.example.com/2"
xmlns:item="http://www.example.com/3"
expand-text="yes">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-skip"/>
<!-- Parse JSON to XML -->
<xsl:template match="data">
<storage:one>
<xsl:apply-templates select="json-to-xml(.)"/>
</storage:one>
</xsl:template>
<xsl:template match="*[@key='storage']">
<!-- Startbase -->
<!-- <xsl:for-each select="*"> -->
<!-- Test[2] -->
<xsl:for-each select="*[local-name() != 'milk']">
<!-- Test[3] -->
<xsl:for-each select="*[not(self::*/@key=milk)]">
<xsl:element name="item:{@key}">
<xsl:attribute name="office">plant-1</xsl:attribute>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:transform>
结果:
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
xmlns:office="http://www.example.com/2"
xmlns:storage="http://www.example.com/1">
<item:pencils office="plant-1">12</item:pencils>
<item:milk office="plant-1">8</item:milk>
<item:rulers office="plant-1">4</item:rulers>
</storage:one>
想要的结果:
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
xmlns:office="http://www.example.com/2"
xmlns:storage="http://www.example.com/1">
<item:pencils office="plant-1">12</item:pencils>
<item:rulers office="plant-1">4</item:rulers>
</storage:one>
解决方法
将 for-each 元素调整为以下将排除指定的元素:
<xsl:for-each select="*[not(@key='milk')]">
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。