XSL 转换在 for-each 期间找不到



我正在尝试为一组 ISO 19115 代码列表的 XML 创建一个 XSL 转换表,因此我可以将它们很好地格式化为单独的文档。但是,到目前为止,我的 XSL 在尝试使用第一个 for-each "循环"后没有生成任何内容。但是,我已经设法使类似结构的"测试"文档正常工作。

我正在尝试解析的 XML 文件的简短版本:

<?xml version="1.0" encoding="UTF-8"?>
<CT_CodelistCatalogue xmlns="http://www.isotc211.org/2005/gmx" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.isotc211.org/2005/gmx ../../gmx/gmx.xsd http://www.isotc211.org/2005/gco ../../gco/gco.xsd http://www.opengis.net/gml ../../gml/gml.xsd http://www.w3.org/1999/xlink ../../xlink/xlinks.xsd">
<codelistItem>
<CodeListDictionary gml:id="CI_DateTypeCode">
<gml:description>identification of when a given event occurred</gml:description>
<gml:identifier codeSpace="ISOTC211/19115">CI_DateTypeCode</gml:identifier>
<codeEntry>
<CodeDefinition gml:id="CI_DateTypeCode_creation">
<gml:description>date identifies when the resource was brought into existence</gml:description>
<gml:identifier codeSpace="ISOTC211/19115">creation</gml:identifier>
</CodeDefinition>
</codeEntry>
<codeEntry>
<CodeDefinition gml:id="CI_DateTypeCode_publication">
<gml:description>date identifies when the resource was issued</gml:description>
<gml:identifier codeSpace="ISOTC211/19115">publication</gml:identifier>
</CodeDefinition>
</codeEntry>
<codeEntry>
<CodeDefinition gml:id="CI_DateTypeCode_revision">
<gml:description>date identifies when the resource was examined or re-examined and improved or amended</gml:description>
<gml:identifier codeSpace="ISOTC211/19115">revision</gml:identifier>
</CodeDefinition>
</codeEntry>
</CodeListDictionary>
</codelistItem>
</CT_CodelistCatalogue>

我正在使用的 XSL 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.isotc211.org/2005/gmx" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.isotc211.org/2005/gmx ../../gmx/gmx.xsd http://www.isotc211.org/2005/gco ../../gco/gco.xsd http://www.opengis.net/gml ../../gml/gml.xsd http://www.w3.org/1999/xlink ../../xlink/xlinks.xsd">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Codelists</title>
</head>
<body>
The first table should be below this.
<xsl:for-each select="CT_CodelistCatalogue/codelistItem/CodeListDictionary">
We have found an item and are creating a list of elements. NB: It never gets to this line
<table>
<xsl:for-each select="codeEntry/CodeDefinition">
<tr>
<td><xsl:value-of select="gml:identifier"/></td>
<td><xsl:value-of select="gml:description"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet> 

此 XSL 生成以下 HTML 输出。请注意,甚至没有"表"调用,因此 for-each 无法找到匹配的元素。

<html xmlns="http://www.isotc211.org/2005/gmx"
xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head>
<title>Codelists</title>
</head>
<body>
The first table should be below this.
</body>
</html>

为了解决我的问题,我在 w3schools 的演示环境中乱搞并制作了一个结构类似的 XML - 只是元素名称不同,没有属性。

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<title>My catalog</title>
<disc>
<cd>
<title>Empire Burlesque</title>
<firstname>blank</firstname>
<lastname>dude</lastname>
<performer>
<artist>
<firstname>Bob</firstname>
<lastname>Dylan</lastname>
</artist>
</performer>
<performer>
<artist>
<firstname>Job</firstname>
<lastname>Bylan</lastname>
</artist>
</performer>
</cd>
</disc>
<disc>
<cd>
<title>Hide your heart</title>
<firstname>test</firstname>
<lastname>case</lastname>
<performer>
<artist>
<firstname>Bonnie</firstname>
<lastname>Tyler</lastname>
</artist>
</performer>
</cd>
</disc>
</catalog>

和 XSL,它正确生成输出。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:for-each select="catalog/disc/cd">
<h3><xsl:value-of select="title" /></h3>
<table border="1">
<xsl:for-each select="performer/artist">
<tr>
<td><xsl:value-of select="firstname" /></td>
<td><xsl:value-of select="lastname" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

上面的 XML 和 XSL 生成了我预期的表格输出。

<body>
<h2>My CD Collection</h2>
<h3>Empire Burlesque</h3>
<table border="1">
<tbody>
<tr>
<td>Bob</td><td>Dylan</td>
</tr>
<tr>
<td>Job</td><td>Bylan</td>
</tr>
</tbody>
</table>
<h3>Hide your heart</h3>
<table border="1">
<tbody>
<tr>
<td>Bonnie</td><td>Tyler</td>
</tr>
</tbody>
</table>
</body>

我真的希望这是我犯的一些简单错误,但我已经卡了一段时间,可能会使用一些外部建议。

谢谢!

区别很简单 - 原始 xml 具有默认的命名空间声明xmlns="http://www.isotc211.org/2005/gmx"因此,除非您也使用该命名空间,否则您的循环将找不到任何内容。

例如,在 xsl 的根目录中使用它作为xmlns:gmx="http://www.isotc211.org/2005/gmx",然后像<xsl:for-each select="gmx:CT_CodelistCat...一样使用它

相关内容

最新更新