问题
I want to add the sublist inside item and footnote text inside list para:
My source xml:
<body>
<p>blahblah</p>
<ul outputclass="l1">
<li outputclass="lt1">blahblah</li>
<li outputclass="lt1">blahblah</li>
<li outputclass="lt1">blahblah
<ul outputclass="l2">
<li outputclass="lt2">blahblah</li>
<li outputclass="lt2">blahblah<fn><p>blah</p></fn></li>
<li outputclass="lt2">blahblah
<ul outputclass="l3">
<li outputclass="lt3">blahblah<fn><p>blah</p></fn></li>
<li outputclass="lt3">blah<fn><p>blah</p></fn>blah</li>
<li outputclass="lt3">blahblah</li>
</ul></li>
</ul></li>
<li outputclass="lt1">blahblah</li>
<li outputclass="lt1">blahblah</li>
</ul>
<p>blahblah</p>
</body>
myxslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="p">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:template match="ul[@outputclass='l1']">
<itemizedlist type="•">
<xsl:apply-templates/>
</itemizedlist>
</xsl:template>
<xsl:template match="ul[@outputclass='l2']">
<itemizedlist type="•">
<xsl:apply-templates/>
</itemizedlist>
</xsl:template>
<xsl:template match="ul[@outputclass='l3']">
<itemizedlist type="•">
<xsl:apply-templates/>
</itemizedlist>
</xsl:template>
<xsl:template match="li[@outputclass='lt1']">
<item>
<xsl:apply-templates/>
</item>
</xsl:template>
<xsl:template match="li[@outputclass='lt2']">
<item>
<xsl:apply-templates/>
</item>
</xsl:template>
<xsl:template match="li[@outputclass='lt3']">
<item>
<xsl:apply-templates/>
</item>
</xsl:template>
<xsl:template match="li/text()[normalize-space()]">
<para>
<xsl:value-of select="."/>
</para>
</xsl:template>
</xsl:stylesheet>
output i am getting para closing at the end of the sub list needed as para closing after sublist:
<body>
<para>blahblah</para>
<itemizedlist type="•">
<item><para>blahblah</para></item>
<item><para>blahblah</para></item>
<item><para>blahblah</para>
<itemizedlist type="•">
<item><para>blahblah</para></item>
<item><para>blahblah</para><footnote><para>blah</para></footnote></item>
<item><para>blahblah</para>
<itemizedlist type="•">
<item><para>blahblah</para><footnote><para>blah</para></footnote></item>
**<item><para>blah</para><footnote><para>blah</para></footnote><para>blah</para></item>**
<item><para>blahblah</para></item>
</itemizedlist></item>
</itemizedlist></item>
<item><para>blahblah</para></item>
<item><para>blahblah</para></item>
</itemizedlist>
<para>blahblah</para>
</body>
but needed output as like sublist should be between para closing and item closing and footnote inside item-para as shown below :
<body>
<para>blahblah</para>
<itemizedlist type="•">
<item><para>blahblah</para></item>
<item><para>blahblah</para></item>
**<item><para>blahblah</para>**
<itemizedlist type="•">
<item><para>blahblah</para></item>
<item><para>blahblah</para></item>
**<item><para>blahblah<footnote><para>blah</para></footnote></para>**
<itemizedlist type="•">
<item><para>blahblah<footnote><para>blah</para></footnote></para></item>
**<item><para>blah<footnote><para>blah</para></footnote>blah</para></item>**
<item><para>blahblah</para></item>
**</itemizedlist></item>**
**</itemizedlist></item>**
<item><para>blahblah</para></item>
<item><para>blahblah</para></item>
</itemizedlist>
<para>blahblah</para>
</body>
Is it possible as bolded one. If possible please suggest me
Thanks in Advance.
回答1:
You haven't really explained which are the criteria to wrap nodes into para
, here is a sample using for-each-group group-adjacent="boolean(self::ul)"
to wrap any adjacent nodes not being ul
elements into para
elements:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:template match="ul[@outputclass='l1'] | ul[@outputclass='l2'] | ul[@outputclass='l3']">
<itemizedlist type="•">
<xsl:apply-templates/>
</itemizedlist>
</xsl:template>
<xsl:template match="li[@outputclass='lt1'] | li[@outputclass='lt2'] | li[@outputclass='lt3']">
<item>
<xsl:for-each-group select="node()" group-adjacent="boolean(self::ul)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()"/>
</xsl:when>
<xsl:otherwise>
<para>
<xsl:apply-templates select="current-group()" mode="preserve"/>
</para>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</item>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/45709058/need-to-add-sublist-in-between-closing-para-and-item-and-footnote-inside-closing