简体   繁体   中英

XSLT: <apply-templates select=“…”>

I have a question regarding <xsl:apply-templates> .

Lets assume I have an XML like this (see http://www.tei-c.org/release/doc/tei-p5-doc/en/html/ref-subst.html ):

<transcription>
<subst>    
    <del>wrong</del>
    <add>right</add>
</subst>    
</transcription>

Now I want to process this recording of a transcription in different ways using XSLT.

If I just want to present the correction to the user, I could use an XSLT template like this:

<xsl:template match="subst"><xsl:apply-templates select="./add"/></xsl:template>

<xsl:template match="subst/add"><xsl:apply-templates/></xsl:template>

However, I could also write:

<xsl:template match="subst"><xsl:apply-templates/></xsl:template>

<xsl:template match="subst/add"><xsl:apply-templates/></xsl:template>

<!-- del: ignore contents -->
<xsl:template match="subst/del"></xsl:template>

In the first case, I explicitly only address add inside <subst> , ignoring <del> .

In the second case, I ignore <del> by providing a template that does not do anything with the element, resulting in the same effect.

If I am not mistaken, the two ways are equivalent. Which one is preferable?

IMHO, not processing nodes at all is preferable to processing them with an empty template. But sometimes the alternative is more convenient, eg for reasons of code readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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