我试图使用以下xslt查询xml文件:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:bpmn="http://dkm.fbk.eu/index.php/BPMN_Ontology">
<!-- Participants -->
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="Package/Participants/Participant">
<tr>
<td><xsl:value-of select="ParticipantType" /></td>
<td><xsl:value-of select="Description" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>以下是xml文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xpdl2bpmn.xsl"?>
<Package xmlns="http://www.wfmc.org/2008/XPDL2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Id="25ffcb89-a9bf-40bc-8f50-e5afe58abda0" Name="1 price setting" OnlyOneProcess="false">
<PackageHeader>
<XPDLVersion>2.1</XPDLVersion>
<Vendor>BizAgi Process Modeler.</Vendor>
<Created>2010-04-24T10:49:45.3442528+02:00</Created>
<Description>1 price setting</Description>
<Documentation />
</PackageHeader>
<RedefinableHeader>
<Author />
<Version />
<Countrykey>CO</Countrykey>
</RedefinableHeader>
<ExternalPackages />
<Participants>
<Participant Id="008af9a6-fdc0-45e6-af3f-984c3e220e03" Name="customer">
<ParticipantType Type="RESOURCE" />
<Description />
</Participant>
<Participant Id="1d2fd8b4-eb88-479b-9c1d-7fe6c45b910e" Name="clerk">
<ParticipantType Type="ROLE" />
<Description />
</Participant>
</Participants>
</Package>尽管,简单的模式,预见是行不通的。Package/Participants/Participant有什么问题?我在这里错过了什么?有什么关于名称空间我不明白的地方吗?
非常感谢!
发布于 2010-04-24 16:18:30
代码中存在许多问题
XML文档的元素在默认名称空间中,但XSLT代码中的匹配模式(和选择表达式)使用"no namespace".
<xsl:value-of>指令试图生成ParticipantType和Description的值“中的元素,但这两个元素没有任何价值。第二个问题需要修改文档,以便ParticipantType和Description具有值。
第一个问题是许多FAQ的主题,并且有一个众所周知的解决方案: XSLT中默认的名称空间--还必须在XSLT样式表中定义并与前缀相关联。在引用XML文档中的名称时,必须使用此前缀。
更正之后,XSLT样式表将如下所示:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:bpmn="http://dkm.fbk.eu/index.php/BPMN_Ontology"
xmlns:xp="http://www.wfmc.org/2008/XPDL2.1"
>
<!-- Participants -->
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="xp:Package/xp:Participants/xp:Participant">
<tr>
<td><xsl:value-of select="xp:ParticipantType" /></td>
<td><xsl:value-of select="xp:Description" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>注意到是新定义的带有xp:前缀的命名空间。
现在的输出是
<html xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:bpmn="http://dkm.fbk.eu/index.php/BPMN_Ontology" xmlns:xp="http://www.wfmc.org/2008/XPDL2.1">
<body>
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>,您只需要解决问题1,,<td>就不会是空的。
发布于 2010-04-24 16:11:40
XML文件具有默认的命名空间。您需要在XSLT中显式地匹配它:
<xsl:for-each
xmlns:xpdl2="http://www.wfmc.org/2008/XPDL2.1"
select="xpdl2:Package/xpdl2:Participants/xpdl2:Participant">
<tr>
<td><xsl:value-of select="xpdl2:ParticipantType" /></td>
<td><xsl:value-of select="xpdl2:Description" /></td>
</tr>
</xsl:for-each>发布于 2010-04-24 19:37:40
Dimitre和bkail指出了XSLT中的问题,并且<ParticipantType>和<Description>元素是空的。
但我想知道,您是否打算获得<ParticipantType>元素的(空)值,而不是它的Type属性(例如,“资源”和“角色”)。在这种情况下,需要更改匹配模式以指定属性:
<td><xsl:value-of select="xp:ParticipantType/@Type" /></td由于示例中的<Description>元素既没有元素内容,也没有任何属性,所以在结果文件中没有什么可以从源中提取出来。但请注意,"content“是对元素开始标记和结束标记之间文本的特定引用,有时您希望包含元素的属性之一的值,而不是”内容“。
Roger_S
https://stackoverflow.com/questions/2705074
复制相似问题