Kri*_*rgh 3 xml xslt foreach pattern-matching
我正在尝试使用以下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>
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
尽管如此,简单的模式,foreach不起作用.什么是错的封装/参与者/参与者?我在这里想念什么?有没有关于命名空间的东西我没有得到?
非常感谢!
Dim*_*hev 10
您的代码中存在许多问题:
XML文档的元素位于默认命名空间中,但XSLT代码中的匹配模式(和选择表达式)使用"无命名空间"中的元素.
这两个<xsl:value-of>指令的试生产出的价值ParticipantType和Description,但是这两个要素不具有任何价值.
第二个问题,需要的XML文档进行更改,以便ParticipantType和Description具有价值.
第一个问题是许多常见问题解答的主题,并且有一个众所周知的解决方案:XML文档中默认的命名空间 - 还必须定义并与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>
Run Code Online (Sandbox Code Playgroud)
注意新定义的名称空间带有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>
Run Code Online (Sandbox Code Playgroud)
你只需要解决问题1,<td>s就不会是空的.
| 归档时间: |
|
| 查看次数: |
2765 次 |
| 最近记录: |