我在xml文档中有一组兄弟元素需要使用XSLT处理成表,实际上我使用Apache FOP将其转换为pdf。只要遇到两种类型的元素之一,就需要创建表行。行中的单元格包含导致在第一个单元格中创建行的元素,然后是后续单元格中的后续兄弟,直到导致创建行的下一个元素。这里有一些示例xml可以更好地解释它:
<pre class="prettyprint"><code class="language-xml">
<reqpers>
<person man="Four"/>
<person man="A" id="pers_a"/>
<perscat category="Recovery Supervisor"/>
<person man="B" id="pers_b"/>
<perscat category="Ground Personnel"/>
<asrequir/>
<perscat category="As Required Category"/>
<trade>Bill Collector</trade>
<person man="C" id="pers_c"/>
<perscat category="Ground Personnel"/>
<perskill skill="sk01"/>
<trade>welder</trade>
<esttime>.5 hr</esttime>
<asrequir/>
<perscat category="2nd Required Category"/>
<esttime>4 days</esttime>
<person man="D" id="pers_d"/>
<perscat category="Rides in Chase Vehicle"/>
<perskill skill="sk02"/>
<person man="E"/>
<perscat category="Jack of all Trades"/>
<trade>engine mechanic</trade>
</reqpers>
</code>
</pre>
需要为每个人或asrequir元素创建一行,然后该行的单元格填充这些元素之间的兄弟组。 我看了很多例子,包括:How to select siblings
和这一个:How to select group of siblings
以及许多其他人。这些都不涉及我的特定问题集,这主要是在XPath查询中有两个后续兄弟要处理的事实。 以下是表格的示例:
<pre class="prettyprint"><code class="language-xml">
<table>
<table-header>
<table-row>
<table-cell> Person </table-cell>
<table-cell> Category/Trade </table-cell>
<table-cell> Skill level </table-cell>
<table-cell> Trade code </table-cell>
<table-cell> Estimated time </table-cell>
</table-row>
</table-header>
<table-body>
<table-row>
<table-cell>Four</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>A</table-cell>
<table-cell>Recovery Supervisor</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>B</table-cell>
<table-cell>Ground Personnel</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>As required</table-cell>
<table-cell/>
<table-cell/>
<table-cell>Bill Collector</table-cell>
<table-cell/>
</table-row>
<table-row>
<table-cell>C</table-cell>
<table-cell>Ground Personnel</table-cell>
<table-cell>skill gets converted to string value</table-cell>
<table-cell>welder</table-cell>
<table-cell>.5 hr</table-cell>
</table-row>
<table-row>
<table-cell>As required</table-cell>
<table-cell>2nd Required Category</table-cell>
<table-cell/>
<table-cell/>
<table-cell>4 days</table-cell>
</table-row>
<table-row>
<table-cell>D</table-cell>
<table-cell>Rides in Chase Vehicle</table-cell>
<table-cell>skill level</table-cell>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>E</table-cell>
<table-cell>Jack of all Trades</table-cell>
<table-cell/>
<table-cell>engine mechanic</table-cell>
<table-cell/>
</table-row>
</table-body>
</table>
</code>
</pre>
同样为了完整性,这是xml我处理的模式片段:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
<xs:element name="reqpers" type="reqpersType"/>
<xs:complexType name="reqpersType">
<xs:sequence>
<xs:element minOccurs="0" ref="applic"/>
<xs:sequence maxOccurs="unbounded">
<xs:choice>
<xs:element ref="asrequir"/>
<xs:element ref="person"/>
</xs:choice>
<xs:sequence minOccurs="0">
<xs:element ref="perscat"/>
<xs:element minOccurs="0" ref="perskill"/>
<xs:element minOccurs="0" ref="trade"/>
<xs:element minOccurs="0" ref="esttime"/>
</xs:sequence>
</xs:sequence>
</xs:sequence>
<xs:attribute ref="refapplic"/>
<xs:attributeGroup ref="bodyatt"/>
<xs:attributeGroup ref="cntlcontent"/>
</xs:complexType>
这是xsl代码的一个例子,它是我最接近的代码,但只有在有兄弟姐妹的情况下才会有效,一旦asrequir被添加的东西分崩离析。我基本上为每个单元格重复这个代码,用我期待的元素替换perscat。
<xsl:for-each select="person | asrequir">
<fo:table-row>
<fo:table-cell text-align="left" padding-before="1mm" padding-after="1mm" padding-left="1mm" padding-right="1mm">
<fo:block font-size="10pt">
<xsl:value-of select="self::person/@man"/>
</fo:block>
</fo:table-cell>
<fo:table-cell text-align="left" padding-before="1mm" padding-after="1mm" padding-left="1mm" padding-right="1mm">
<xsl:variable name="perscatSib" select="following-sibling::perscat"/>
<xsl:variable name="perscatPrec" select="following-sibling::person[1]/preceding-sibling::perscat"/>
<fo:block font-size="10pt">
<xsl:choose>
<xsl:when test="$perscatSib[count(. | $perscatPrec) = count($perscatPrec)]">
<xsl:value-of select="$perscatSib[count(. | $perscatPrec) = count($perscatPrec)]/@category"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="preceding-sibling::person[1] and not(following-sibling::person[1])">
<xsl:value-of select="following-sibling::perscat[1]/@category"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</fo:block>
</fo:table-cell>
<continues with the rest of the cells../>
这是我生产的表格:
<pre class="prettyprint"><code class="language-xml">
<table>
<table-header>
<table-row>
<table-cell> Person </table-cell>
<table-cell> Category/Trade </table-cell>
<table-cell> Skill level </table-cell>
<table-cell> Trade code </table-cell>
<table-cell> Estimated time </table-cell>
</table-row>
</table-header>
<table-body>
<table-row>
<table-cell>Four</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>A</table-cell>
<table-cell>Recovery Supervisor</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>B</table-cell>
<table-cell>Ground Personnel</table-cell>
<table-cell/>
**<table-cell>BillCollector</table-cell>**
<table-cell/>
</table-row>
<table-row>
<table-cell>As required</table-cell>
<table-cell/>
<table-cell/>
<table-cell>Bill Collector</table-cell>
<table-cell/>
</table-row>
<table-row>
<table-cell>C</table-cell>
<table-cell>Ground Personnel</table-cell>
<table-cell>skill gets converted to string value</table-cell>
<table-cell>welder</table-cell>
<table-cell>.5 hr</table-cell>
</table-row>
<table-row>
<table-cell>As required</table-cell>
<table-cell>2nd Required Category</table-cell>
<table-cell/>
<table-cell/>
<table-cell>4 days</table-cell>
</table-row>
<table-row>
<table-cell>D</table-cell>
<table-cell>Rides in Chase Vehicle</table-cell>
<table-cell>skill level</table-cell>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>E</table-cell>
<table-cell>Jack of all Trades</table-cell>
<table-cell/>
<table-cell>engine mechanic</table-cell>
<table-cell/>
</table-row>
</table-body>
</table>
</code>
</pre>
围绕它的**表格单元格中不应该有数据..它添加了以下asrequir的值,这不是我想要的......
我尝试添加以下兄弟::人[&#39; 1&#39;] | asrequir [&#39; 1&#39;]但是asrequir总是如此,所以我在我不喜欢的地方得到额外的兄弟姐妹想要他们。任何关于如何解决这个问题的见解或建议都将非常感激。我怀疑我需要使用某种类型的分组或密钥集,但我不确定如何实现它们。虽然不是XSLT和XPath的新手,但我不是专家。
答案 0 :(得分:2)
A key may help which group all elements from reqpers
which are not person or asrequir
to the preceding-sibling person or asrequir.
<xsl:key name="kperson" match="reqpers/*[not(self::person or self::asrequir)]"
use="generate-id(preceding-sibling::*[self::person or self::asrequir][1])
The group stats with:
<xsl:for-each select="*[self::person or self::asrequir]">
The group members are then:
<xsl:variable name="this" select="." />
<xsl:variable name="group" select=". | key('kperson',generate-id($this))" />
Get a value from the group:
<xsl:value-of select="$group[self::perscat]/@category"/>
You may try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="kperson" match="reqpers/*[not(self::person or self::asrequir)]"
use="generate-id(preceding-sibling::*[self::person or self::asrequir][1])"/>
<xsl:template match="reqpers">
<table>
<table-header>
<table-row>
<table-cell> Person </table-cell>
<table-cell> Category/Trade </table-cell>
<table-cell> Skill level </table-cell>
<table-cell> Trade code </table-cell>
<table-cell> Estimated time </table-cell>
</table-row>
</table-header>
<xsl:for-each select="*[self::person or self::asrequir]">
<xsl:variable name="this" select="." />
<xsl:variable name="group" select=". | key('kperson',generate-id($this))" />
<table-row>
<table-cell>
<xsl:value-of select="$group[self::person]/@man"/>
<xsl:if test="$group[self::asrequir]" >As required</xsl:if>
</table-cell>
<table-cell><xsl:value-of select="$group[self::perscat]/@category"/></table-cell>
<table-cell><xsl:value-of select="$group[self::perskill]/@skill"/></table-cell>
<table-cell><xsl:value-of select="$group[self::trade]"/></table-cell>
<table-cell><xsl:value-of select="$group[self::esttime]"/></table-cell>
</table-row>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
With following output:
<table>
<table-header>
<table-row>
<table-cell> Person </table-cell>
<table-cell> Category/Trade </table-cell>
<table-cell> Skill level </table-cell>
<table-cell> Trade code </table-cell>
<table-cell> Estimated time </table-cell>
</table-row>
</table-header>
<table-row>
<table-cell>Four</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>A</table-cell>
<table-cell>Recovery Supervisor</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>B</table-cell>
<table-cell>Ground Personnel</table-cell>
<table-cell/>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>As required</table-cell>
<table-cell>As Required Category</table-cell>
<table-cell/>
<table-cell>Bill Collector</table-cell>
<table-cell/>
</table-row>
<table-row>
<table-cell>C</table-cell>
<table-cell>Ground Personnel</table-cell>
<table-cell>sk01</table-cell>
<table-cell>welder</table-cell>
<table-cell>.5 hr</table-cell>
</table-row>
<table-row>
<table-cell>As required</table-cell>
<table-cell>2nd Required Category</table-cell>
<table-cell/>
<table-cell/>
<table-cell>4 days</table-cell>
</table-row>
<table-row>
<table-cell>D</table-cell>
<table-cell>Rides in Chase Vehicle</table-cell>
<table-cell>sk02</table-cell>
<table-cell/>
<table-cell/>
</table-row>
<table-row>
<table-cell>E</table-cell>
<table-cell>Jack of all Trades</table-cell>
<table-cell/>
<table-cell>engine mechanic</table-cell>
<table-cell/>
</table-row>
</table>