xref: /aosp_15_r20/external/apache-xml/test/tests/contrib/xsltc/mk/mk044.xsl (revision 1212f9a0ffdc28482b8821715d2222bf16dc14e2)
1<xsl:transform
2 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 version="1.0"
4>
5
6  <!-- Test FileName: mk044.xsl -->
7  <!-- Source Attribution:
8       This test was written by Michael Kay and is taken from
9       'XSLT Programmer's Reference' published by Wrox Press Limited in 2000;
10       ISBN 1-861003-12-9; copyright Wrox Press Limited 2000; all rights reserved.
11       Now updated in the second edition (ISBN 1861005067), http://www.wrox.com.
12       No part of this book may be reproduced, stored in a retrieval system or
13       transmitted in any form or by any means - electronic, electrostatic, mechanical,
14       photocopying, recording or otherwise - without the prior written permission of
15       the publisher, except in the case of brief quotations embodied in critical articles or reviews.
16  -->
17  <!-- Example: authors.xml, word-count.xsl -->
18  <!-- Chapter/Page: 7-494 -->
19  <!-- Purpose: Using normalize-space to get a word count -->
20
21<xsl:template name="word-count">
22    <xsl:param name="text"/>
23    <xsl:variable name="ntext" select="normalize-space($text)"/>
24    <xsl:choose>
25    <xsl:when test="$ntext">
26        <xsl:variable name="remainder">
27            <xsl:call-template name="word-count">
28                <xsl:with-param name="text" select="substring-after($ntext, ' ')"/>
29            </xsl:call-template>
30        </xsl:variable>
31        <xsl:value-of select="$remainder + 1"/>
32    </xsl:when>
33    <xsl:otherwise>0</xsl:otherwise>
34    </xsl:choose>
35</xsl:template>
36
37<xsl:template match="/">
38    <xsl:for-each select="//*">
39        <xsl:variable name="length">
40            <xsl:call-template name="word-count">
41                <xsl:with-param name="text" select="."/>
42            </xsl:call-template>
43        </xsl:variable>
44        <element name="{name()}" words="{$length}"/>;
45    </xsl:for-each>
46</xsl:template>
47
48  <!--
49   * Licensed to the Apache Software Foundation (ASF) under one
50   * or more contributor license agreements. See the NOTICE file
51   * distributed with this work for additional information
52   * regarding copyright ownership. The ASF licenses this file
53   * to you under the Apache License, Version 2.0 (the  "License");
54   * you may not use this file except in compliance with the License.
55   * You may obtain a copy of the License at
56   *
57   *     http://www.apache.org/licenses/LICENSE-2.0
58   *
59   * Unless required by applicable law or agreed to in writing, software
60   * distributed under the License is distributed on an "AS IS" BASIS,
61   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
62   * See the License for the specific language governing permissions and
63   * limitations under the License.
64  -->
65
66</xsl:transform>
67