Xml 如何使用XPath和XSLT处理一组同级节点,并基于两个同级节点处理特定的同级子集
我在xml文档中有一组同级元素,需要使用XSLT将其处理成一个表,实际上我正在使用ApacheFop将其转换成pdf。每当遇到两种类型的元素之一时,就需要创建表行。行中的单元格由导致在第一个单元格中创建行的元素组成,然后是导致在后续单元格中创建行的下一个元素,直到导致创建行的下一个元素。下面是一些xml示例,可以更好地解释它:Xml 如何使用XPath和XSLT处理一组同级节点,并基于两个同级节点处理特定的同级子集,xml,xslt,xpath,Xml,Xslt,Xpath,我在xml文档中有一组同级元素,需要使用XSLT将其处理成一个表,实际上我正在使用ApacheFop将其转换成pdf。每当遇到两种类型的元素之一时,就需要创建表行。行中的单元格由导致在第一个单元格中创建行的元素组成,然后是导致在后续单元格中创建行的下一个元素,直到导致创建行的下一个元素。下面是一些xml示例,可以更好地解释它: <pre class="prettyprint"><code class="language-xml"> <reqp
<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>
还有这个:
和其他许多人一样。这些都不涉及我的特定问题集,这主要是因为在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>
人
类别/行业
技能水平
行业代码
估计时间
四
A.
恢复主管
B
地勤人员
按要求
收票人
C
地勤人员
技能转换为字符串值
焊接工
.5小时
按要求
第二必选类别
4天
D
乘坐追击车
熟练水平
E
万事通
发动机技工
为了完整起见,这是我正在处理的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>
一个键可能有助于将需求者
中非个人或ASrequirer
的所有元素分组到前面的同级人或ASrequirer
<xsl:key name="kperson" match="reqpers/*[not(self::person or self::asrequir)]"
use="generate-id(preceding-sibling::*[self::person or self::asrequir][1])
您能使用XSLT 2.0吗?对于从开始的每个组,我现在只能使用XSLT 1.0。
<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>
<xsl:key name="kperson" match="reqpers/*[not(self::person or self::asrequir)]"
use="generate-id(preceding-sibling::*[self::person or self::asrequir][1])
<xsl:for-each select="*[self::person or self::asrequir]">
<xsl:variable name="this" select="." />
<xsl:variable name="group" select=". | key('kperson',generate-id($this))" />
<xsl:value-of select="$group[self::perscat]/@category"/>
<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>
<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>