从 foreach 中排除指定元素
Exclude specified element from foreach
我希望使用导入的 JSON 数据的结果映射在 for-each 期间排除一些元素。
我怀疑我是 运行 的测试可能在 运行 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')]">
我希望使用导入的 JSON 数据的结果映射在 for-each 期间排除一些元素。 我怀疑我是 运行 的测试可能在 运行 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')]">