Hairi
Hairi

Reputation: 3725

XSL template match pattern applies to all child nodes

This is a snippet of my stylesheet:

	<xsl:template name="root" match="/">
		<xsl:apply-templates select="/office:document-content/office:body/office:text"/>
	</xsl:template>
	<!-- -->
	<xsl:template name="text" match="office:text">
		<text>
			<xsl:apply-templates/>
		</text>
	</xsl:template>
	<xsl:template match="text:h|text:p">
		<para>
			<xsl:value-of select="1"/>
		</para>
	</xsl:template>

And this is the input xml file. Snippet:

<office:body>
    <office:text text:use-soft-page-breaks="true">
        <text:h text:style-name="P1" text:outline-level="1"><text:bookmark-start text:name="_Toc433035627"/>Права за<text:s/>достъп<text:s/>до ARISTA MDM<text:bookmark-end text:name="_Toc433035627"/></text:h>
        <text:p text:style-name="Normal">Входът в системата се осъществява с потребителско име и парола, като проверката на потребителското име и паролата се извършва в управляващия център ARISTA<text:s/>MDM или чрез активна директория (MS AD). При създаване на нов потребител има възможност да се определи дали вписването да се извършва от активната директория (MS AD) или управляващия център ARISTA MDM.</text:p>
        <text:p text:style-name="Header">Правата<text:s/>на потребителя конфигурират неговия достъп до:</text:p>
        <text:list text:style-name="LFO4" text:continue-numbering="true">
            <text:list-item>
                <text:p text:style-name="ListBullet2">Менюта и полета;</text:p>
            </text:list-item>
            <text:list-item>
                <text:p text:style-name="ListBullet2">Параметри на устройства;</text:p>
            </text:list-item>
            <text:list-item>
                <text:p text:style-name="ListBullet2">Части на структурата (папки, подпапки).<text:s/></text:p>
            </text:list-item>
        </text:list>
        <text:list text:style-name="LFO9" text:continue-numbering="true">
            <text:list-item>
                <text:p text:style-name="ListNumber2">Потребителите наследяват правата на ролите, които притежават. Единственият начин потребителите да получават или да им се отнемат права е чрез добавяне или премахване на<text:s/>ролите;</text:p>
            </text:list-item>
            <text:list-item>
                <text:p text:style-name="ListNumber2">Паролите на потребителите, които се валидират към центъра<text:s/>ARISTA<text:s/>MDM<text:s/>могат да имат период на валидност. Периода на валидност се задава при създаване на потребителя;</text:p>
            </text:list-item>
            <text:list-item>
                <text:p text:style-name="ListNumber2">При изтичане на периода на валидност на паролата, от потребителя се изисква да промени паролата си;</text:p>
            </text:list-item>
            <text:list-item>
                <text:p text:style-name="ListNumber2">Всички действия на потребителите ще бъдат записвани и показвани в табличен вид, като информацията включва: потребителско име, действие, дата и час, модул и допълнителна информация;</text:p>
            </text:list-item>
            <text:list-item>
                <text:p text:style-name="ListNumber2">Записването на потребителските действия се активира или деактивира при създаване на нов потребител или промяна на данните на съществуващ.</text:p>
            </text:list-item>
        </text:list>
        <text:h text:style-name="Heading1" text:outline-level="1"><text:bookmark-start text:name="_Toc414357454"/><text:bookmark-start text:name="_Toc433035628"/>Вписване в ARISTA MDM<text:bookmark-end text:name="_Toc414357454"/><text:bookmark-end text:name="_Toc433035628"/></text:h>
        <text:p text:style-name="Normal">
            <text:span text:style-name="Strong">След въвеждане на адреса на системата се появява прозореца за вписване на потребители.</text:span>
        </text:p>
        <text:p text:style-name="Quote">За вписване в системата, операторът трябва да въведе съответното<text:s/>потребителско име и парола и да натисне бутон.</text:p>
    </office:text>
</office:body>

And this is the output I get:

<text>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
	<para>1</para>
</text>

Please tell me why <xsl:template match="text:h|text:p"> is applied for the other siblings (text:list) elements too? I has been explicitly told to be applied to text:h and text:p only. Excuse my poor English. Thank you for your time.

Upvotes: 2

Views: 109

Answers (2)

Wayne
Wayne

Reputation: 60414

There are two things that together explain the behavior you're seeing.

  • First, realize that <xsl:apply-templates /> applies to all children of the current node whenever the select attribute is missing, so you're applying templates to all element children of matched office:text elements (which includes those text:list elements).

  • Second, although you have no templates that match those text:list elements, XSLT includes built-in templates, which are designed to allow recursive processing to continue whenever there is no successful pattern match, and that do match those elements. The built-in template that does that looks like this:

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

So, what's happening is that the built-in template matches the text:list children (because it matches all elements), then applies templates to its children, which results in further matches by your explicit template.

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117018

Please tell me why <xsl:template match="text:h|text:p"> is applied for the other siblings (text:list) elements too?

What makes you think that it is? If you change:

<xsl:template match="text:h|text:p">
    <para>
        <xsl:value-of select="1"/>
    </para>
</xsl:template>

to:

<xsl:template match="text:h|text:p">
    <para>
        <xsl:value-of select="name()"/>
    </para>
</xsl:template>

you will get a result of:

<text>
   <para>text:h</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:p</para>
   <para>text:h</para>
   <para>text:p</para>
   <para>text:p</para>
</text>

so clearly the text:list elements were not processed.

Upvotes: 0

Related Questions