我想编写一个XSLT来将XML文档转换为XML-FO文档,以生成PDF。
此图片显示current Output
所以我的表有3列。在前4行中,第二列和第三列合并。
我的XML文档如下所示:
<WS-Standards>
<tags>
<tag category = "parameters" > <!-- WS_Beer_Type -->
<tag_name>WS_Beer_TypeX</tag_name> <!-- browsename -->
<tag_number>30004</tag_number>
<datatype>Unsigned32</datatype>
<accessrights>RW</accessrights>
<names>
<name language="DE">Biersorte</name>
<name language="EN">Beer Type</name>
</names>
</tag>
</tags>
</WS-Standards>
我当前的XSLT文档:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="WS-Standards">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master page-height="297mm" page-width="210mm"
margin="5mm 25mm 5mm 25mm" master-name="PageMaster">
<fo:region-body margin="20mm 0mm 20mm 0mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="PageMaster">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<xsl:apply-templates select="tags"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="tags">
<fo:block>
<xsl:apply-templates select="tag"/>
</fo:block>
</xsl:template>
<xsl:template match="tag">
<fo:block space-before="6pt" border-top="3pt solid green">
<fo:table>
<fo:table-column column-number="1" column-width="20%" border-style="solid"
border-width="1pt"/>
<fo:table-column column-number="2" column-width="10%" border-style="solid"
border-width="1pt"/>
<fo:table-column column-number="3" column-width="70%" border-style="solid"
border-width="1pt"/>
<fo:table-body>
<xsl:apply-templates/>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template match="tag_name">
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block> Tag Name </fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" number-columns-spanned="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<xsl:template match="tag_number">
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block> Tag-Nummer </fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" number-columns-spanned="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<xsl:template match="datatype">
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block> Datentyp </fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" number-columns-spanned="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<xsl:template match="accessrights">
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block> Zugriffsrechte </fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" number-columns-spanned="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<xsl:template match="names">
<xsl:choose>
<xsl:when test="number(./name/position()) = 1">
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block> Name </fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="./name/@language"/>
</fo:block>
</fo:table-cell>
<fo:table-cell column-number="3" border="1pt solid black">
<fo:block>
<xsl:value-of select="./name"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:when>
<xsl:otherwise>
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block> NameXXX </fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="./name/@language"/>
</fo:block>
</fo:table-cell>
<fo:table-cell column-number="3" border="1pt solid black">
<fo:block>
<xsl:value-of select="./name"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="p">
<fo:block>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="b">
<fo:inline font-weight="bold">
<xsl:apply-templates/>
</fo:inline>
</xsl:template>
</xsl:stylesheet>
我的问题是关于不同语言的名称元素。
因此,names元素表中的第一行如下所示:
-----------------------------
|Name | DE | Biersorte |
-----------------------------
但我错过了第二行:
-----------------------------
| | EN | beer type |
-----------------------------
xsl我做错了什么:choose-&gt; when-&gt;否则函数?
我很乐意提示。
答案 0 :(得分:1)
相关代码位于匹配names
的模板中,但XSLT中只有一个这样的元素,因此只会调用一次。
此上下文中的表达式number(./name/position()) = 1
只是询问&#34;对于此names
元素,位置1和#34;中是否有name
元素,这是真的,并且所以xsl:when
被执行了。
您的代码确实需要位于选中name
的块中。试试这个XSLT(它也删除了代码重复)。
<xsl:template match="names">
<xsl:for-each select="name">
<fo:table-row>
<fo:table-cell column-number="1" border="1pt solid black">
<fo:block>
<xsl:if test="position() = 1">Name</xsl:if>
</fo:block>
</fo:table-cell>
<fo:table-cell column-number="2" border="1pt solid black">
<fo:block>
<xsl:value-of select="@language"/>
</fo:block>
</fo:table-cell>
<fo:table-cell column-number="3" border="1pt solid black">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</xsl:template>