简体   繁体   English

xslt无法正常工作,匹配变量

[英]xslt not working as expected, match variable

I'm working on an xslt for processing fix messages (I've already been assisted with this a bit.) I'm trying to retain all fields that have been referred to in various ways. 我正在研究用于处理修复消息的xslt(我已经获得了一些帮助。)我正在尝试保留以各种方式引用的所有字段。 My XSLT is not managing to do this though and I'm finding it very hard to work out why. 我的XSLT并没有做到这一点,我发现很难找出原因。 I've a cut down xslt that retains the field I'm after, but the full xslt does not. 我有一个缩小的xslt,它保留了我需要的字段,但是完整的xslt却没有。 This could be due to my lack of understanding around how variables work but I'm not sure. 这可能是由于我对变量的工作原理缺乏了解,但我不确定。 I'm not an xslt programmer so am finding this quite hard. 我不是xslt程序员,所以很难找到这一点。

Sample xml file 样本xml文件

<fix>
<messages>
    <message name="TheReport" >
        <component name="OuterComp" required="N" />
    </message>
</messages>
<components>
    <component name="OuterComp">
        <field name="AppID" required="N" />
        <component name="InnerComp" required="N" />
    </component>
    <component name="InnerComp">
        <group name="Bah" required="N" >
            <field name="NewField" required="N" />
        </group>
    </component>
</components>
<fields>
    <field number="3" name="AppID" type="STRING" />
    <field number="15" name="Bah" type="STRING" />
    <field number="16" name="NewField" type="STRING" />
</fields>
</fix>

The 'correctly' (as in it manages to retain the bah field) produced xml output “正确”(因为它设法保留了bah字段)产生的xml输出

<?xml version="1.0" encoding="UTF-8"?>
<fix>
<messages>
    <message name="TheReport">
        <component name="OuterComp" required="N" />
    </message>
</messages>
<components>
    <component name="OuterComp">
        <field name="AppID" required="N" />
        <component name="InnerComp" required="N" />
    </component>
    <component name="InnerComp">
        <group name="Bah" required="N">
            <field name="NewField" required="N" />
        </group>
    </component>
</components>
<fields>
    <field number="15" name="Bah" type="STRING" />
</fields>
</fix>

The cut down XSLT that does work 切合实际的XSLT

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:key name="mf" match="message/field" use="@name" />
<xsl:key name="cg" match="component/group" use="@name" />
<xsl:key name="cc" match="component/component" use="@name" />
<xsl:key name="mc" match="message/component" use="@name" />


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

<xsl:template match="fields/field[not(key('mf', @name))]">
    <xsl:variable name="IsUsed">
        <xsl:apply-templates select="key('cg', @name)" mode="IsUsed" />
    </xsl:variable>
    <xsl:if test="$IsUsed != ''">
        <xsl:call-template name="identity" />
    </xsl:if>
</xsl:template>

<xsl:template match="component/group[not(key('mc', ../@name))]" mode="IsUsed">
    <xsl:apply-templates select="key('cc', ../@name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="component/group[key('mc', ../@name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

<xsl:template match="component/component[not(key('mc', ../@name))]" mode="IsUsed">
    <xsl:apply-templates select="key('mc', ../@name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="component/component[key('mc', ../@name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

</xsl:stylesheet> 

and the full one that does not 而完整的不

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:key name="mc" match="message/component" use="@name" />
<xsl:key name="c2" match="group/component" use="@name" />
<xsl:key name="cc" match="component/component" use="@name" />

<xsl:key name="mf" match="message/field" use="@name" />
<xsl:key name="c4" match="group/field" use="@name" />
<xsl:key name="c5" match="group" use="@name" />
<xsl:key name="c7" match="component/field" use="@name" />
<xsl:key name="cg" match="component/group" use="@name" />

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

<xsl:template match="fields/field[not(key('mf', @name))]">
    <xsl:variable name="IsUsed">

        <xsl:apply-templates select="key('c4', @name)" mode="IsUsed" />
        <xsl:apply-templates select="key('c7', @name)" mode="IsUsed" />
        <xsl:apply-templates select="key('c5', @name)" mode="IsUsed" />
        <xsl:apply-templates select="key('c2', @name)" mode="IsUsed" />
        <xsl:apply-templates select="key('cc', @name)" mode="IsUsed" />
        <xsl:apply-templates select="key('cg', @name)" mode="IsUsed" />
    </xsl:variable>
    <xsl:if test="$IsUsed != ''">
        <xsl:call-template name="identity" />
    </xsl:if>
</xsl:template>

<xsl:template match="group/field[not(key('mc', ../../@name))]" mode="IsUsed">
    <xsl:apply-templates select="key('c2', ../../@name)" mode="IsUsed"/>
    <xsl:apply-templates select="key('cc', ../../@name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="group/field[key('mc', ../../@name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

<xsl:template match="component/field[not(key('mc', ../@name))]" mode="IsUsed">
    <xsl:apply-templates select="key('c2', ../@name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="component/field[key('mc', ../@name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

<xsl:template match="component/group[not(key('mc', ../@name))]" mode="IsUsed">
    <xsl:apply-templates select="key('c2', ../@name)" mode="IsUsed"/>
    <xsl:apply-templates select="key('cc', ../@name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="component/group[key('mc', ../@name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

<!-- <xsl:template match="component[not(key('mc', @name))]" mode="IsUsed">
    <xsl:apply-templates select="key('c2', @name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="component[key('mc', @name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template> -->

<xsl:template match="group[not(key('mc', ../@name))]" mode="IsUsed">
    <xsl:apply-templates select="key('c2', ../@name)" mode="IsUsed"/>
</xsl:template>

<xsl:template match="group[key('mc', ../@name)]" mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

<xsl:template match="components/component[not(key('mc', @name))]">
    <xsl:variable name="IsUsed">
        <xsl:apply-templates select="key('cc', @name)" mode="IsUsed" />
        <xsl:apply-templates select="key('c2', @name)" mode="IsUsed" />

    </xsl:variable>
    <xsl:if test="$IsUsed != ''">
        <xsl:call-template name="identity" />
    </xsl:if>
</xsl:template>

<xsl:template match="group/component[not(key('mc', ../../@name))]"
    mode="IsUsed">
    <xsl:apply-templates select="key('c2', ../../@name)"
        mode="IsUsed" />
</xsl:template>

<xsl:template match="group/component[key('mc', ../../@name)]"
    mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

<xsl:template match="component/component[not(key('mc', ../@name))]"
    mode="IsUsed">
    <!-- used to be cc -->
    <xsl:apply-templates select="key('mc', ../@name)" 
        mode="IsUsed" />
</xsl:template>

<xsl:template match="component/component[key('mc', ../@name)]"
    mode="IsUsed">
    <xsl:text>1</xsl:text>
</xsl:template>

</xsl:stylesheet> 

Could I have some assistance in working out what is going wrong? 我可以在解决问题方面获得一些帮助吗?

Thanks. 谢谢。

Managed to work out how to use the Eclipse XSLT debugger and sorted this problem. 设法弄清楚如何使用Eclipse XSLT调试器并解决了此问题。 Was taking a route I didn't expect. 正在走一条我没想到的路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM