确定所有节点和属性中不匹配的值:XSLT2.0

时间:2017-10-24 14:57:35

标签: xslt-2.0

我需要遍历所有xml属性和文本节点,以从列表中识别字符的存在,并输出不匹配的字符值的值。

我能够检查text()节点,但我无法检查属性。

<xsl:template match="@*|node()">
<xsl:variable name="getDelimitersToUseNodes" select="('$' ,'#' ,'*' ,'~')[not(contains(current(),.))]"/>
    <xsl:variable name="getDelimitersToUseAttr" select="string-join(('$','#','*','~')[not(contains(@*/,.))],',')"/>
    <xsl:variable name="getDelimitersToUse" select="concat(string-join($getDelimitersToUseNodes,','),',',string-join($getDelimitersToUseAttr,','))"/>
    <!--xsl:variable name="delim" select="distinct-values($getDelimitersToUse,',')"/-->
    <xsl:value-of select="$getDelimitersToUse"/>
   </xsl:template>

我的模拟示例文件位于

之下
<?xml version="1.0"?>
<sample>
<test1 name="#theGoofy">My$#test</test1>
<test2 value="$#@">description test2*</test2>
</sample>

1 个答案:

答案 0 :(得分:0)

您可以处理所有这些文本和属性节点,并进行与以前相同的检查。假设你可以使用

文本,你还没有真正说出你想要的输出格式
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:param name="characters" as="xs:string*" select="'$' ,'#' ,'*' ,'~'"/>

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <xsl:apply-templates select="//text() | //@*"/>
</xsl:template>

<xsl:template match="text() | @*">
    <xsl:value-of select="'Text', ., 'does not contain', $characters[not(contains(current(), .))], '&#10;'"/>
</xsl:template>

</xsl:stylesheet>

获得像

这样的结果
Text #theGoofy does not contain $ * ~ 
Text My$#test does not contain * ~ 
Text $#@ does not contain * ~ 
Text description test2* does not contain $ # ~ 

如果您只是想检查所有文本节点和属性节点中未包含的所有字符,那么就像

那样
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:param name="characters" as="xs:string*" select="'$' ,'#' ,'*' ,'~'"/>

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="nodes-to-inspect" as="node()*" select="//text() | //@*"/>

<xsl:template match="/">
    <xsl:value-of select="for $c in $characters return $c[not($nodes-to-inspect[contains(., $c)])]"/>
</xsl:template>

</xsl:stylesheet>

应该这样做。

相关问题