1
votes

I need to detect the first title element and print out a special comment before this first title element.

<xsl:template match="title[string-length(.) &gt; 0]" mode="content">
    <xsl:comment>%title id="<xsl:value-of select="."/>"%</xsl:comment>
</xsl:template>

I can detect these title elements, but what i've got to do to count and get this special comment before the first title element?

EDIT: Thanks for the comments, but i can't get it to work unfortunatly. So additionally i add the XML source here, maybe someone could point me to en elegant solution?

<body id="EL_bodyN10001"><text id="EL_textN10001"><page id="EL_pageN10004"><p id="EL_pN10006"></p><p id="EL_pN10012">lorem ipsum dolor sit</p><p id="EL_pN10045">msdfsdkfh sdfsfksd sfsdfsdfsdfdsfh <strong id="EL_strongN1004C">LOREM IPSUM</strong>. lorem aklsjd asda sdasjdlkjaksld</p>
<title id="EL_titleN10001">my first title here</title>
<subheading id="EL_subheadingN1006E" level="1">MY FIRST SUBHEADING</subheading><p id="EL_pN10075">sdfjlksjfsjdf sfsjfljsdf sfklöskdfö sdfölksföksölf sdfksfk</p><p id="EL_pN10084">tzputozu lultpülupützu tultlutzulztu wewe rrtrt.</p><title id="EL_titleN10001">my second title</title><subheading id="EL_subheadingN100D9" level="1">THIS IS MY SECONT SUBHEADING</subheading><p id="EL_pN100E0">this is another senseless paragraph test.</p></page></text></body>
2
Are all title elements siblings of the same parent? Then match="title[normalize-space()][1]" suffices. Or you could check there is no preceding::title. - Martin Honnen
Yes, the elements are alle siblings of the same parent. Thanks for the hint. I tried it this way: <xsl:template match="title[string-length(.) &gt; 0]" mode="content"> <xsl:choose> <xsl:when test="title[normalize-space()][1]"> <xsl:comment>XXX FIRST %title id="<xsl:value-of select="."/>"%</xsl:comment> </xsl:when> <xsl:otherwise> <xsl:comment>%title id="<xsl:value-of select="."/>"%</xsl:comment> </xsl:otherwise> </xsl:choose> </xsl:template> Doesn't work and i don't know why - michbeck
What are you counting? - Flynn1179
I try to count the title element with pos() but the first occurance says pos = 7, the socond pos=15..there must be another way - michbeck

2 Answers

0
votes

Try

<xsl:template match="title[string-length(.) &gt; 0][1]" mode="content"> <xsl:comment>XXX FIRST %title id="<xsl:value-of select="."/>"%</xsl:comment> </xsl:template>

then write a second template match="title[string-length(.) &gt; 0][position() > 1]" with the other content.

0
votes

finally the solution was using:

<xsl:variable name="subHeadingPos" select="count(./preceding-sibling::subheading)+1"/>

Thanks to you, guys