XSLT-FO 应用模板
XSLT-FO apply-templates
我有一个 XML 文件
<results xmlns="zwierzeta">
<animal family="kurowate" genus="tetrao">
<name>skrzekot</name>
<food>wszystkożerca</food>
</animal>
<animal family="kurowate" genus="lyrurus">
<name>cietrzew</name>
<status>zagrożony</status>
<food>wszystkożerca</food>
<lifespan>15</lifespan>
</animal>
<animal family="kurowate" genus="tetrao">
<name>głuszec</name>
<status>zagrożony</status>
<food>wszystkożerca</food>
<lifespan>12</lifespan>
</animal>
</results>
并尝试制作一个简单的 table 三列:姓名、状态、食物。
我的 xsl 文件:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns="zwierzeta" xmlns:a="anatomia">
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="wzorzec1" page-width="210mm" page-height="297mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
<fo:region-body margin="3cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="wzorzec1">
<fo:flow flow-name="xsl-region-body" font-family="Arial">
<fo:block font-size="20" text-align="center">Raport</fo:block>
<fo:table border="1pt solid black" text-align="center" border-spacing="2pt">
<fo:table-header>
<fo:table-row>
<fo:table-cell border="1pt solid black"> <fo:block>Nazwa</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Status</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Odzywianie</fo:block> </fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="animal"/>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="animal">
<fo:table-row>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="name"/></fo:block>
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="status"/></fo:block>
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="food"/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>
这给了我一个错误:
Not valid: org.apache.fop.fo.ValidationException: "fo:table-body" is missing child elements. Required content model: marker* (table-row+|table-cell+) (See position 1:975)
我的理解是应用模板部分没有被评估的问题。但我不知道为什么会这样。我尝试复制正常工作的不同文件并更改元素名称,但没有帮助。
显然是你的默认命名空间
xmlns="zwierzeta"
不被接受。将其更改为带前缀的命名空间可以解决您的问题。所以改成
xmlns:zw="zwierzeta"
然后将前缀添加到依赖于默认命名空间的所有元素。整个 XSLT 文件可能如下所示:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:zw="zwierzeta" xmlns:a="anatomia">
<xsl:template match="/zw:results"> <!-- Prefixed -->
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="wzorzec1" page-width="210mm" page-height="297mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
<fo:region-body margin="3cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="wzorzec1">
<fo:flow flow-name="xsl-region-body" font-family="Arial">
<fo:block font-size="20" text-align="center">Raport</fo:block>
<fo:table border="1pt solid black" text-align="center" border-spacing="2pt">
<fo:table-header>
<fo:table-row>
<fo:table-cell border="1pt solid black"> <fo:block>Nazwa</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Status</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Odzywianie</fo:block> </fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="zw:animal"/>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="zw:animal"> <!-- Prefixed -->
<fo:table-row>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="zw:name"/></fo:block> <!-- Prefixed -->
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="zw:status"/></fo:block> <!-- Prefixed -->
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="zw:food"/></fo:block> <!-- Prefixed -->
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>
我有一个 XML 文件
<results xmlns="zwierzeta">
<animal family="kurowate" genus="tetrao">
<name>skrzekot</name>
<food>wszystkożerca</food>
</animal>
<animal family="kurowate" genus="lyrurus">
<name>cietrzew</name>
<status>zagrożony</status>
<food>wszystkożerca</food>
<lifespan>15</lifespan>
</animal>
<animal family="kurowate" genus="tetrao">
<name>głuszec</name>
<status>zagrożony</status>
<food>wszystkożerca</food>
<lifespan>12</lifespan>
</animal>
</results>
并尝试制作一个简单的 table 三列:姓名、状态、食物。
我的 xsl 文件:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns="zwierzeta" xmlns:a="anatomia">
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="wzorzec1" page-width="210mm" page-height="297mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
<fo:region-body margin="3cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="wzorzec1">
<fo:flow flow-name="xsl-region-body" font-family="Arial">
<fo:block font-size="20" text-align="center">Raport</fo:block>
<fo:table border="1pt solid black" text-align="center" border-spacing="2pt">
<fo:table-header>
<fo:table-row>
<fo:table-cell border="1pt solid black"> <fo:block>Nazwa</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Status</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Odzywianie</fo:block> </fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="animal"/>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="animal">
<fo:table-row>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="name"/></fo:block>
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="status"/></fo:block>
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="food"/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>
这给了我一个错误:
Not valid: org.apache.fop.fo.ValidationException: "fo:table-body" is missing child elements. Required content model: marker* (table-row+|table-cell+) (See position 1:975)
我的理解是应用模板部分没有被评估的问题。但我不知道为什么会这样。我尝试复制正常工作的不同文件并更改元素名称,但没有帮助。
显然是你的默认命名空间
xmlns="zwierzeta"
不被接受。将其更改为带前缀的命名空间可以解决您的问题。所以改成
xmlns:zw="zwierzeta"
然后将前缀添加到依赖于默认命名空间的所有元素。整个 XSLT 文件可能如下所示:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:zw="zwierzeta" xmlns:a="anatomia">
<xsl:template match="/zw:results"> <!-- Prefixed -->
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="wzorzec1" page-width="210mm" page-height="297mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
<fo:region-body margin="3cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="wzorzec1">
<fo:flow flow-name="xsl-region-body" font-family="Arial">
<fo:block font-size="20" text-align="center">Raport</fo:block>
<fo:table border="1pt solid black" text-align="center" border-spacing="2pt">
<fo:table-header>
<fo:table-row>
<fo:table-cell border="1pt solid black"> <fo:block>Nazwa</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Status</fo:block> </fo:table-cell>
<fo:table-cell border="1pt solid black"> <fo:block>Odzywianie</fo:block> </fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="zw:animal"/>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="zw:animal"> <!-- Prefixed -->
<fo:table-row>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="zw:name"/></fo:block> <!-- Prefixed -->
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="zw:status"/></fo:block> <!-- Prefixed -->
</fo:table-cell>
<fo:table-cell border="1pt solid black">
<fo:block><xsl:value-of select="zw:food"/></fo:block> <!-- Prefixed -->
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>