DesignLiquido / xslt-processor

A JavaScript XSLT processor without native library dependencies
GNU Lesser General Public License v3.0
94 stars 30 forks source link

Multiple match #49

Closed marcobalestra closed 12 months ago

marcobalestra commented 2 years ago

In case of multiple node matches, instead of applying to the closest match all of the matches are applied at the same time.

e.g:

    <xsl:template match="*">
        <node name="{name(.)" />
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates select="*" />
    </xsl:template>

    <xsl:template match="creationTime" />

    <xsl:template match="/*/submissionTime">
        <foo>bar</foo>
        <xsl:apply-templates select="*" />
    </xsl:template>

In this case

I also tried by assigning priority to template matches, but it didn't solve the issue.

leonelsanchesdasilva commented 12 months ago

Hi @marcobalestra. Can you please share with the a sample XML?

marcobalestra commented 12 months ago

Of course @leonelsanchesdasilva …

consider this XML:

<root>
    <typeA />  
    <typeB />  
</root>

and this XSLT:

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

    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" />

    <xsl:template match="*|/*">
        <outputUnknown original-name="{name(.)}">
            <xsl:apply-templates select="*" />
        </outputUnknown>
    </xsl:template>

    <xsl:template match="typeA">
        <outputA />
    </xsl:template>

    <xsl:template match="/*/typeB">
        <outputB>I have text!</outputB>
    </xsl:template>

</xsl:stylesheet>

Expected output:

<outputUnknown original-name="root">
    <outputA />
    <outputB>I have text!</outputB>
</outputUnknown>

Actual output:

<outputUnknown original-name="root">
    <outputA original-name="typeA"/>
    <outputB original-name="typeB"></outputB>
</outputUnknown>

You can see that in both cases (typeA and typeB) the result is actually a mix of the first template and the one (2nd or 3rd) that is expected to be the only to match.

In addition…

Furthermore, and this seems to be another bug, if the first template only matches <xsl:template match="*"> (that is expected to match /* as well) the output is expected to be the same initially expected, while it changes again:

<outputB></outputB>

I'm a big fan of XSLT, ready and willing to support.
All tests were performed using the static example provided.

leonelsanchesdasilva commented 12 months ago

@marcobalestra Thanks, this helps me enormously.

I should work on these bugs now. Will let you know when this is ready.

Happy to have your support!

leonelsanchesdasilva commented 11 months ago

@marcobalestra This problem is now solved accordingly at https://github.com/DesignLiquido/xslt-processor/releases/tag/v1.2.0.