XSLT用于过滤带有字母的记录
我们需要过滤数字字段中带有字符的记录,并单独报告它们。我确实遇到了下面的问题,这个问题已经得到了回答- 但是,有没有一种方法可以用标志标记这些记录,或者在变量中收集它们,因为我们需要将这些记录报告为无效记录。如果我们完全删除它们,问题是我们不知道哪一个是无效的 请建议 谢谢大家! 输入:XSLT用于过滤带有字母的记录,xslt,filter,records,Xslt,Filter,Records,我们需要过滤数字字段中带有字符的记录,并单独报告它们。我确实遇到了下面的问题,这个问题已经得到了回答- 但是,有没有一种方法可以用标志标记这些记录,或者在变量中收集它们,因为我们需要将这些记录报告为无效记录。如果我们完全删除它们,问题是我们不知道哪一个是无效的 请建议 谢谢大家! 输入: <?xml version="1.0" encoding="UTF-8"?> <payload> <records> <record>
<?xml version="1.0" encoding="UTF-8"?>
<payload>
<records>
<record>
<number>123</number>
</record>
<record>
<number>456</number>
</record>
<record>
<number>78A</number>
</record>
</records>
</payload>
123
456
78A
输出:
<?xml version="1.0" encoding="UTF-8"?>
<payload>
<records>
<record>
<number>123</number>
</record>
<record>
<number>456</number>
</record>
</records>
</payload>
123
456
来自上面链接的XSLT解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="record[translate(number, '0123456789', '')]"/>
</xsl:stylesheet>
匹配完成后,使用您想要的任何“标志”(属性、注释、处理指令等)输出原始元素 例如:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="record[string(number(number))='NaN']">
<record invalid="true">
<xsl:apply-templates select="@*|node()"/>
</record>
</xsl:template>
</xsl:stylesheet>
更新了XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="record">
<xsl:variable name="invalidCols">
<xsl:apply-templates select="*" mode="invalid"/>
</xsl:variable>
<record>
<xsl:if test="string($invalidCols)">
<xsl:attribute name="invalidCols">
<xsl:value-of select="normalize-space($invalidCols)"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</record>
</xsl:template>
<xsl:template match="number[string(number(.))='NaN']" mode="invalid">
<xsl:number/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="*" mode="invalid"/>
</xsl:stylesheet>
输出
<payload>
<records>
<record>
<number>123</number>
</record>
<record>
<number>456</number>
</record>
<record invalidCols="2 4">
<number>321</number>
<number>78A</number>
<number>654</number>
<number>abc</number>
<number>123456</number>
</record>
</records>
</payload>
123
456
321
78A
654
abc
123456
谢谢你,丹尼尔!是否可以在记录级别而不是字段级别进行标记?请suggest@user3219481-我在记录
级别将我的答案更新为标志。这个变化应该很明显,所以如果你对正在发生的事情有疑问,请让我知道。谢谢你,丹尼尔!这真的对我有帮助。是否可以确定哪一列存在问题?如果我在一个包含多个字段的记录中设置属性,它将发现该记录,但是否有方法识别记录和字段的组合。请给出建议。@user3219481-我添加了另一个示例,但我假设number
=列。你没有说你想要什么,所以我在记录
级别输出“列号”。我的错。有效载荷为123 456 321 78A abc 56B
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="record">
<xsl:variable name="invalidCols">
<xsl:apply-templates select="*" mode="invalid"/>
</xsl:variable>
<record>
<xsl:if test="string($invalidCols)">
<xsl:attribute name="invalidCols">
<xsl:value-of select="normalize-space($invalidCols)"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</record>
</xsl:template>
<xsl:template match="number[string(number(.))='NaN']" mode="invalid">
<xsl:number/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="*" mode="invalid"/>
</xsl:stylesheet>
<payload>
<records>
<record>
<number>123</number>
</record>
<record>
<number>456</number>
</record>
<record invalidCols="2 4">
<number>321</number>
<number>78A</number>
<number>654</number>
<number>abc</number>
<number>123456</number>
</record>
</records>
</payload>