作为列表和子列表格式

时间:2017-06-21 08:35:20

标签: xml xslt

我希望para的列表格式子列表也是para格式的列表格式

我的输入XML文件:

<?xml version="1.0" encoding="UTF-8"?>


<chapter>
<title>Base Food</title>
<subsection>
<title>Nothing</title>
<body>
<p>  (a)<tab/>1Y The Act also states that the may undertake a review of the definition of the term.</p>
<p>  (b)<tab/>The Act also states that the may undertake a review of the definition of the term:</p>
<p>  (i)<tab/>Act also states that the may undertake a review of the definition of the term.</p>
<p>  (ii)<tab/>States that the may undertake a review of the definition of the term.</p>
</body>
</subsection>
</chapter>

我的XSLT编码是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" omit-xml-declaration="no"></xsl:output>

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


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

<xsl:template match="subsection">
<section>
<xsl:apply-templates/>
</section>
</xsl:template>

<xsl:template match="p">
<para>
 <xsl:apply-templates/>
</para>
</xsl:template>

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

</xsl:stylesheet>


</xsl:stylesheet>

我的输出为

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<chapter>
<title>Base Food</title>
<section>
<title>Nothing</title>
<para>  (a)<tab/>1Y The Act also states that the may undertake a review of the definition of the term.</para>
<para>  (b)<tab/>The Act also states that the may undertake a review of the definition of the term:</para>
<para>  (i)<tab/>Act also states that the may undertake a review of the definition of the term.</para>
<para>  (ii)<tab/>States that the may undertake a review of the definition of the term.</para>
</section>
</chapter>

但我想在文本之前输出制表符标签作为项目属性

需要输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<chapter>
<title>Base Food</title>
<section level="sect1" num="I" number-type="manual">
<title>Nothing</title>
<orderedlist type="manual">
<item num="(a)"><para>1Y The Act also states that the may undertake a review of the definition of the term.</para></item>
<item num="(b)"><para>The Act also states that the may undertake a review of the definition of the term:</para>
<orderedlist type="manual">
<item num="(i)"><para>Act also states that the may undertake a review of the definition of the term.</para></item>
<item num="(ii)"><para>States that the may undertake a review of the definition of the term.</para></item></orderedlist></item></orderedlist>
</section>
</chapter>

请帮助我。

先谢谢

1 个答案:

答案 0 :(得分:0)

尝试编写匹配body的模板,首先使用for-each-group group-adjacent标识相邻的p元素,然后使用递归函数使用{{1}查找嵌套列表}:

for-each-group group-starting-with

我已经将其编写为Saxon 9.8支持的XSLT 3.0,但您应该能够将除<?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" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:mf="http://example.com/mf" exclude-result-prefixes="xs math mf" version="3.0"> <xsl:param name="patterns" as="xs:string*" select="'^\s*(\(a\))', '^\s*(\(i\))'"/> <xsl:output indent="yes"/> <xsl:mode on-no-match="shallow-copy"/> <xsl:function name="mf:list-group" as="node()*"> <xsl:param name="input" as="element()*"/> <xsl:sequence select="mf:list-group($input, $patterns)"/> </xsl:function> <xsl:function name="mf:list-group" as="node()*"> <xsl:param name="input" as="node()*"/> <xsl:param name="patterns" as="xs:string*"/> <xsl:choose> <xsl:when test="$patterns[1]"> <xsl:for-each-group select="$input" group-starting-with="p[matches(., $patterns[1])]"> <xsl:choose> <xsl:when test="self::p[matches(., $patterns[1])]"> <orderedlist type="manual"> <xsl:sequence select="mf:list-group(current-group(), $patterns[position() gt 1])"></xsl:sequence> </orderedlist> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="current-group()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="$input"/> </xsl:otherwise> </xsl:choose> </xsl:function> <xsl:template match="body"> <xsl:for-each-group select="*" group-adjacent="boolean(self::p[node()[1][self::text()[matches(., '^\s*\([a-z]+\)$')]] and node()[2][self::tab]])"> <xsl:choose> <xsl:when test="current-grouping-key()"> <xsl:sequence select="mf:list-group(current-group())"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="current-group()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:template> <xsl:template match="p"> <item num="{replace(node()[1], '^\s+', '')}"> <para><xsl:apply-templates select="node()[position() gt 2]"/></para> </item> </xsl:template> </xsl:stylesheet> 之外的所有内容合并到XSLT 2.0样式表中,并使用其他模板进行已有的转换。