Xml xslt:测试父节点级别的任何节点是否具有特定值

Xml xslt:测试父节点级别的任何节点是否具有特定值,xml,xslt,xpath,Xml,Xslt,Xpath,这里我有简化版的xml,我想使用子节点中的IMP值(如果有的话)填写父节点中的数据,如果子节点中的IMP值为true,则应该为true。如果不存在子项,那么它应该是假的 <info> <parent index='0'> <name>test1</name> <children> </children> </parent> <paren

这里我有简化版的xml,我想使用子节点中的IMP值(如果有的话)填写父节点中的数据,如果子节点中的IMP值为true,则应该为true。如果不存在子项,那么它应该是假的

<info>
    <parent index='0'>
        <name>test1</name>
        <children>
        </children>
    </parent>
    <parent index='1'>
        <name>test2</name>
        <children>
            <VALUE index='0'>test3</VALUE>
            <VALUE index='1'>test4</VALUE>
        </children>
    </parent>
    <parent index='2'>
        <name>test3</name>
        <impvalue>true</impvalue>
    </parent>
    <parent index='3'>
        <name>test4</name>
        <impvalue>false</impvalue>
    </parent>
</info>

测试1
测试2
测试3
测试4
测试3
符合事实的
测试4
错误的
所需的输出是

<info>
    <parent index='0'>
        <name>test1</name>
        <children>
        </children>
        <impvalue>false</impvalue>
    </parent>
    <parent index='1'>
        <name>test2</name>
        <children>
            <VALUE index='0'>test3</VALUE>
            <VALUE index='1'>test4</VALUE>
        </children>
        <impvalue>true</impvalue>
    </parent>
    <parent index='2'>
        <name>test3</name>
        <impvalue>true</impvalue>
    </parent>
    <parent index='3'>
        <name>test4</name>
        <impvalue>false</impvalue>
    </parent>
</info>

测试1
错误的
测试2
测试3
测试4
符合事实的
测试3
符合事实的
测试4
错误的

由于输入和输出XML相似,您可以使用身份转换从复制输入数据开始

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
完整的XSLT如下所示

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*"/>

    <!-- identity transform -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <!--  match <parent> having no <impvalue> child -->
    <xsl:template match="parent[not(impvalue)]">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
            <impvalue>
                <xsl:choose>
                    <xsl:when test="count(children/*) != 0">
                        <xsl:value-of select="'true'" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="'false'" />
                    </xsl:otherwise>
                </xsl:choose>
            </impvalue>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出

<info>
    <parent index="0">
        <name>test1</name>
        <children />
        <impvalue>false</impvalue>
    </parent>
    <parent index="1">
        <name>test2</name>
        <children>
            <VALUE index="0">test3</VALUE>
            <VALUE index="1">test4</VALUE>
        </children>
        <impvalue>true</impvalue>
    </parent>
    <parent index="2">
        <name>test3</name>
        <impvalue>true</impvalue>
    </parent>
    <parent index="3">
        <name>test4</name>
        <impvalue>false</impvalue>
    </parent>
</info>

测试1
错误的
测试2
测试3
测试4
符合事实的
测试3
符合事实的
测试4
错误的

共享的输入XML不是格式良好的XML。请关闭
标记进行更正,并为
提供正确的标记名称。标记名中不应包含空格。如果
value
的一个属性,请提供它的值。修复了它,这只是一个示例,因为我无法提供真实的数据
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*"/>

    <!-- identity transform -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <!--  match <parent> having no <impvalue> child -->
    <xsl:template match="parent[not(impvalue)]">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
            <impvalue>
                <xsl:choose>
                    <xsl:when test="count(children/*) != 0">
                        <xsl:value-of select="'true'" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="'false'" />
                    </xsl:otherwise>
                </xsl:choose>
            </impvalue>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
<info>
    <parent index="0">
        <name>test1</name>
        <children />
        <impvalue>false</impvalue>
    </parent>
    <parent index="1">
        <name>test2</name>
        <children>
            <VALUE index="0">test3</VALUE>
            <VALUE index="1">test4</VALUE>
        </children>
        <impvalue>true</impvalue>
    </parent>
    <parent index="2">
        <name>test3</name>
        <impvalue>true</impvalue>
    </parent>
    <parent index="3">
        <name>test4</name>
        <impvalue>false</impvalue>
    </parent>
</info>