2016-10-10 64 views
1

我需要检测第一个标题元素并在此第一个标题元素之前打印出特殊注释。XSL变换:计数元素

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

我可以检测到这些标题元素,但是我需要做些什么来计算并在第一个标题元素之前获得此特殊评论?

编辑: 感谢您的意见,但我不能让它工作不幸。所以,另外我在这里添加XML源代码,也许有人可以指出我优雅的解决方案?

<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> 
+0

是否所有'同一母公司的title'元素的兄弟姐妹?然后'match =“title [normalize-space()] [1]”'就足够了。或者你可以检查没有'之前::标题'。 –

+0

是的,元素是同一父母的同胞兄弟姐妹。 感谢您的提示。我尝试了这种方式: 的 \t \t \t \t \t的 \t \t \t \t XXX FIRST%标题ID =” 的 “% \t \t \t \t \t \t \t \t \t \t \t %标题ID = “的” % \t \t \t \t \t \t \t 不起作用,我不知道为什么 – michbeck

+0

你在计算什么? – Flynn1179

回答

0

最后的解决方案是使用:

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

谢谢你,伙计们

0

尝试

<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>

然后写第二个模板match="title[string-length(.) &gt; 0][position() > 1]"与其他内容。