您好我想将我的xml转换为我的标题分组
这是我的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<header1>
<title>Head 1</title>
<sub>
<title>sub 1</title>
</sub>
<sub>
<title>sub 2</title>
</sub>
</header1>
</root>
这是我的xslt文件:
<xsl:template match="header1">
<fo:block>
<xsl:number level="multiple" count="header" format="1"/>
<xsl:value-of select="./title/text()"/>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="sub">
<fo:block>
<xsl:number level="multiple" count="sub" format="1.1"/>
<xsl:value-of select="./title/text()"/>
</fo:block>
</xsl:template>
预期输出为:
1 Head
1.1 Head - sub 1
1.2 Head - sub 2
现在输出:
Head1 Head 1
1sub 1
2sub 2
答案 0 :(得分:1)
首先,您的标题元素名为header1
,而不是header
。计算header
元素将始终产生意外结果。
要使xsl:number
指望多个级别,您需要指定应计算的元素,并将其与|
分开。下面是一个生成格式良好的XSL-FO文档的完整示例。
在您当前的输出中,文字太多了。这是因为您需要使用空模板覆盖文本节点的内置模板,匹配text()
。
XSLT样式表
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/root">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="page"
page-height="297mm" page-width="210mm"
margin-top="20mm" margin-bottom="10mm"
margin-left="25mm" margin-right="25mm">
<fo:region-body
margin-top="0mm" margin-bottom="15mm"
margin-left="0mm" margin-right="0mm"/>
<fo:region-after extent="10mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="header1">
<fo:block>
<xsl:number level="multiple" count="header1" format="1 "/>
<xsl:value-of select="title"/>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="sub">
<fo:block>
<xsl:number level="multiple" count="sub|header1" format="1. "/>
<xsl:value-of select="concat('Head - ',title)"/>
</fo:block>
</xsl:template>
<xsl:template match="text()"/>
</xsl:transform>
XSL-FO输出
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="page"
page-height="297mm"
page-width="210mm"
margin-top="20mm"
margin-bottom="10mm"
margin-left="25mm"
margin-right="25mm">
<fo:region-body margin-top="0mm"
margin-bottom="15mm"
margin-left="0mm"
margin-right="0mm"/>
<fo:region-after extent="10mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page">
<fo:flow flow-name="xsl-region-body">
<fo:block>1 Head 1<fo:block>1.1. Head - sub 1</fo:block>
<fo:block>1.2. Head - sub 2</fo:block>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
呈现PDF输出
在线试用此解决方案here并阅读XSLT中的编号,例如: this excellent XML.com article