XSL for-each过滤:排除Gebruik为GEPLAND及无该属性的节点
解决方案:XSL筛选符合条件的Placemark并生成插入语句
核心过滤条件
在XPath中,通过以下条件筛选存在Gebruik属性且属性值不为GEPLAND的Placemark节点:
Root/Placemark[@Gebruik and @Gebruik != 'GEPLAND']
@Gebruik:判断节点是否包含该属性,直接排除无此属性的节点@Gebruik != 'GEPLAND':排除属性值为GEPLAND的节点- 两者用
and连接,确保同时满足两个条件
完整示例代码
XML示例
<Root> <Placemark ID="1" Gebruik="ACTIEF">Sample Content 1</Placemark> <Placemark ID="2" Gebruik="GEPLAND">Sample Content 2</Placemark> <Placemark ID="3">Sample Content 3</Placemark> <Placemark ID="4" Gebruik="INACTIEF">Sample Content 4</Placemark> </Root>
XSL转换代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" encoding="UTF-8"/> <xsl:template match="/"> <!-- 遍历符合过滤条件的Placemark --> <xsl:for-each select="Root/Placemark[@Gebruik and @Gebruik != 'GEPLAND']"> <!-- 生成数据库INSERT语句,直接引用节点的ID属性 --> INSERT INTO your_table (id, gebruik) VALUES (<xsl:value-of select="@ID"/>, '<xsl:value-of select="@Gebruik"/>'); <xsl:text> </xsl:> <!-- 添加换行,让输出的SQL语句更易读 --> </xsl:for-each> </xsl:template> </xsl:stylesheet>
输出结果
运行上述XSL转换后,会生成以下SQL插入语句:
INSERT INTO your_table (id, gebruik) VALUES (1, 'ACTIEF'); INSERT INTO your_table (id, gebruik) VALUES (4, 'INACTIEF');
可以看到,ID为2(GEPLAND)和ID为3(无Gebruik属性)的节点被自动过滤,且生成的语句中ID与原节点完全对应。
内容的提问来源于stack exchange,提问作者Severid
相关产品推荐
相关产品推荐

