xsl 在带有样式表的 Multiple 的 xml 文档上创建
Posted
技术标签:
【中文标题】xsl 在带有样式表的 Multiple 的 xml 文档上创建【英文标题】:xsl Create on xml document from Multiple with style sheet 【发布时间】:2017-08-18 20:16:50 【问题描述】:我确信这很简单,但我无法做我需要做的事情。将不胜感激一些帮助。它应该发生在 cmets 中,下面是我所做的。在样式表下方,我复制了 3 个 xml 文件作为调试示例。感谢它,因为我无法弄清楚。
我有多个 xml 文档。每个文件名的格式为 dept 加上部门编号和 .xml(即 depta00.xml)。我需要使用所有 xml 文件中的数据创建一个 horizons.xml 文件,其中列出了每个部门的所有员工。每个文档的格式都相同。具体来说,我需要在样式表中执行以下操作:
-
创建一个名为 getEmployees 的模板
在 getPeople 模板中,创建一个名为 depts 的变量,其中包含一系列代表部门代码的以下文本字符串:'a00','c01','d11','d21','e11 '和'e21'
在创建 depts 变量的行之后,创建 departments 元素。
在 departments 元素中,插入一个
for-each
循环,循环遍历 depts 序列中的每个条目。
对于 depts 序列中的每个条目,请执行以下操作:
一个。创建一个变量名称 currentDept 等于 depts 序列中的当前项。
b.创建一个元素部门,其属性名为 deptID,其值等于 currentDept 变量的值。
c。使用doc()
函数引用deptcurrent.xml 文件,其中current 是currentDept 变量的值。使用concat()
函数将dept 的文本字符串、currentDept 变量和文本字符串“.xml”结合起来。
d。使用copy-of
元素将employees 元素及其后代元素的内容复制到department 元素。
XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template name ="getEmployees">
<xsl:variable name="depts" select="('a00','c01','d11','d21','e11', 'e21')" as="xs:string*" />
<xsl:element name="departments">
<xsl:for-each select="$depts"> <xsl:value-of select="." />
<xsl:variable name="currentDept"> <xsl:value-of select="$depts" /> </xsl:variable>
<xsl:element name="department">
<xsl:attribute name="deptID"> <xsl:value-of select="$currentDept" /> </xsl:attribute>
<xsl:value-of select="doc(concat('dept',$currentDept, '.xml'))" />
<xsl:copy-of select="employees" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
XML 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department A00
Filename: depta00.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="10">
<firstName>Marylin</firstName>
<middleInt>A</middleInt>
<lastName>Johnson</lastName>
<department>A00</department>
<phone>3978</phone>
<email>Johnson.60@example.com/horizons</email>
<dateHired>2000-01-01</dateHired>
<title>President</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1968-08-24</birthDate>
<salary>121300</salary>
<bonus>2300</bonus>
<commission>9700</commission>
</employee>
<employee empID="40">
<firstName>Heather</firstName>
<middleInt>D</middleInt>
<lastName>Gordon</lastName>
<department>A00</department>
<phone>3915</phone>
<email>Gordon.59@example.com/horizons</email>
<dateHired>2009-03-01</dateHired>
<title>Manager</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1986-06-03</birthDate>
<salary>85400</salary>
<bonus>1700</bonus>
<commission>6500</commission>
</employee>
</employees>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department E11
Filename: depte11.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="50">
<firstName>Alicia</firstName>
<middleInt>J</middleInt>
<lastName>Silva</lastName>
<department>E11</department>
<phone>6789</phone>
<email>Silva.50@example.com/horizons</email>
<dateHired>2013-08-17</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>M</gender>
<birthDate>1960-09-15</birthDate>
<salary>92400</salary>
<bonus>1800</bonus>
<commission>7400</commission>
</employee>
<employee empID="90">
<firstName>Ila</firstName>
<middleInt>R</middleInt>
<lastName>Leclerc</lastName>
<department>E11</department>
<phone>5498</phone>
<email>Leclerc.51@example.com/horizons</email>
<dateHired>2005-08-15</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1976-05-15</birthDate>
<salary>68400</salary>
<bonus>1400</bonus>
<commission>5500</commission>
</employee>
</employees>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department D21
Filename: deptd21.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="70">
<firstName>William</firstName>
<middleInt>P</middleInt>
<lastName>Kimble</lastName>
<department>D21</department>
<phone>7831</phone>
<email>Kimble.87@example.com/horizons</email>
<dateHired>2015-09-30</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1988-05-26</birthDate>
<salary>83200</salary>
<bonus>1600</bonus>
<commission>6700</commission>
</employee>
<employee empID="230">
<firstName>Kevin</firstName>
<middleInt>J</middleInt>
<lastName>Smith</lastName>
<department>D21</department>
<phone>2094</phone>
<email>Smith.77@example.com/horizons</email>
<dateHired>2001-11-21</dateHired>
<title>Clerk</title>
<edLevel>14</edLevel>
<gender>M</gender>
<birthDate>1970-05-30</birthDate>
<salary>51000</salary>
<bonus>900</bonus>
<commission>4100</commission>
</employee>
<employee empID="240">
<firstName>Patrick</firstName>
<middleInt>M</middleInt>
<lastName>Trexler</lastName>
<department>D21</department>
<phone>3780</phone>
<email>Trexler.63@example.com/horizons</email>
<dateHired>2014-12-05</dateHired>
<title>Clerk</title>
<edLevel>17</edLevel>
<gender>M</gender>
<birthDate>1989-03-31</birthDate>
<salary>66100</salary>
<bonus>1400</bonus>
<commission>5300</commission>
</employee>
</employees>
【问题讨论】:
希望这些不是真实姓名和数据!这是一个公共网站。 此外,显示所需的 XML 输出可能比在长列表中显示更能说明问题。 【参考方案1】:考虑使用外部exsl:node-set()
保存部门代码树,然后使用xsl:for-each
运行。请注意,以下仅贯穿您发布的部门。扩展 $deptList 以获取必须存在于样式表当前目录中的其他可用 deptXXX.xml。请注意,您可以通过避免 <xsl:element>
或 <xsl:attribute>
调用来减少代码:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:call-template name="getEmployees"/>
</xsl:template>
<xsl:template name="getEmployees">
<xsl:variable name="deptList">
<depts>
<dept>a00</dept>
<dept>d21</dept>
<dept>e11</dept>
</depts>
</xsl:variable>
<xsl:variable name="depts" select="ext:node-set($deptList)/depts/*"/>
<departments>
<xsl:for-each select="$depts">
<xsl:variable name="currentDept"><xsl:value-of select="."/></xsl:variable>
<department dept="$currentDept">
<xsl:if test="doc(concat('DeptEmployees',$currentDept, '.xml'))">
<xsl:copy-of select="doc(concat('dept',$currentDept, '.xml'))/employees" />
</xsl:if>
</department>
</xsl:for-each>
</departments>
</xsl:template>
</xsl:stylesheet>
输出
<?xml version="1.0" encoding="UTF-8"?>
<departments>
<department dept="a00">
<employees>
<employee empID="10">
<firstName>Marylin</firstName>
<middleInt>A</middleInt>
<lastName>Johnson</lastName>
<department>A00</department>
<phone>3978</phone>
<email>Johnson.60@example.com/horizons</email>
<dateHired>2000-01-01</dateHired>
<title>President</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1968-08-24</birthDate>
<salary>121300</salary>
<bonus>2300</bonus>
<commission>9700</commission>
</employee>
<employee empID="40">
<firstName>Heather</firstName>
<middleInt>D</middleInt>
<lastName>Gordon</lastName>
<department>A00</department>
<phone>3915</phone>
<email>Gordon.59@example.com/horizons</email>
<dateHired>2009-03-01</dateHired>
<title>Manager</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1986-06-03</birthDate>
<salary>85400</salary>
<bonus>1700</bonus>
<commission>6500</commission>
</employee>
</employees>
</department>
<department dept="d21">
<employees>
<employee empID="70">
<firstName>William</firstName>
<middleInt>P</middleInt>
<lastName>Kimble</lastName>
<department>D21</department>
<phone>7831</phone>
<email>Kimble.87@example.com/horizons</email>
<dateHired>2015-09-30</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1988-05-26</birthDate>
<salary>83200</salary>
<bonus>1600</bonus>
<commission>6700</commission>
</employee>
<employee empID="230">
<firstName>Kevin</firstName>
<middleInt>J</middleInt>
<lastName>Smith</lastName>
<department>D21</department>
<phone>2094</phone>
<email>Smith.77@example.com/horizons</email>
<dateHired>2001-11-21</dateHired>
<title>Clerk</title>
<edLevel>14</edLevel>
<gender>M</gender>
<birthDate>1970-05-30</birthDate>
<salary>51000</salary>
<bonus>900</bonus>
<commission>4100</commission>
</employee>
<employee empID="240">
<firstName>Patrick</firstName>
<middleInt>M</middleInt>
<lastName>Trexler</lastName>
<department>D21</department>
<phone>3780</phone>
<email>Trexler.63@example.com/horizons</email>
<dateHired>2014-12-05</dateHired>
<title>Clerk</title>
<edLevel>17</edLevel>
<gender>M</gender>
<birthDate>1989-03-31</birthDate>
<salary>66100</salary>
<bonus>1400</bonus>
<commission>5300</commission>
</employee>
</employees>
</department>
<department dept="e11">
<employees>
<employee empID="50">
<firstName>Alicia</firstName>
<middleInt>J</middleInt>
<lastName>Silva</lastName>
<department>E11</department>
<phone>6789</phone>
<email>Silva.50@example.com/horizons</email>
<dateHired>2013-08-17</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>M</gender>
<birthDate>1960-09-15</birthDate>
<salary>92400</salary>
<bonus>1800</bonus>
<commission>7400</commission>
</employee>
<employee empID="90">
<firstName>Ila</firstName>
<middleInt>R</middleInt>
<lastName>Leclerc</lastName>
<department>E11</department>
<phone>5498</phone>
<email>Leclerc.51@example.com/horizons</email>
<dateHired>2005-08-15</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1976-05-15</birthDate>
<salary>68400</salary>
<bonus>1400</bonus>
<commission>5500</commission>
</employee>
</employees>
</department>
</departments>
【讨论】:
以上是关于xsl 在带有样式表的 Multiple 的 xml 文档上创建的主要内容,如果未能解决你的问题,请参考以下文章