【发布时间】:2017-11-19 12:19:11
【问题描述】:
我尝试使用 XSLT 从 API XML 响应文件中选择属性。
这是返回 XML 文件的请求:http://zoekdienst.overheid.nl/sru/Search?version=1.2&operation=searchRetrieve&x-connection=oep&startRecord=1&maximumRecords=10&query=(keyword=verkeersbesluit) and (creator=groningen) and (organisationType=Provincie)
我创建了以下 XSL 文件:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<verkeersbesluiten>
<test>Hello world!</test>
<numberOfRecords><xsl:value-of select="searchRetrieveResponse/numberOfRecords"/></numberOfRecords>
<xsl:for-each select="searchRetrieveResponse/records/record/recordData/gzd/originalData/overheidop:meta/overheidop:owmskern">
<besluit>
<test2>Hello world again!</test2>
<titel><xsl:value-of select="dcterms:title"/></titel>
<locatie><xsl:value-of select="dcterms:spatial"/></locatie>
</besluit>
</xsl:for-each>
</verkeersbesluiten>
</xsl:template>
</xsl:stylesheet>
转换后的结果文件:
<?xml version="1.0" encoding="UTF-8"?>
<verkeersbesluiten xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<test>Hello world!</test>
<numberOfRecords/>
</verkeersbesluiten>
这不是我想要的结果。那将是:
<?xml version="1.0" encoding="UTF-8"?>
<verkeersbesluiten xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<test>Hello world!</test>
<numberOfRecords>41</numberOfRecords>
<besluit>
<test2>Hello World again!</test2>
<titel>Provincie Groningen: verkeersbesluit nieuwe situatie parallelweg langs N964 tussen Winschoten en Scheemda.</titel>
<locatie>264455 576268$%$Oldambt</locatie>
</besluit>
<besluit>
<test2>Hello World again!</test2>
<titel>Provincie Groningen: verkeersbesluit nieuwe situatie N366, Westerstraat en A.G. Wildervanckweg in Ter Apel</titel>
<locatie>266425 544276$%$Vlagtwedde<</locatie>
</besluit>
...
</verkeersbesluiten>
有人可以指出我做错了什么吗?
威利
================================================ =============================
正如@halfbit 所建议的,我已将命名空间 srw 和 sru 添加到我的 XSL 文件中:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<verkeersbesluiten>
<test>Hello world!</test>
<numberOfRecords><xsl:value-of select="srw:searchRetrieveResponse/srw:numberOfRecords"/></numberOfRecords>
<xsl:for-each select="srw:searchRetrieveResponse/srw:records/srw:record/sru:gzd">
<besluit>
<test2>Hello world again!</test2>
</besluit>
</xsl:for-each>
</verkeersbesluiten>
</xsl:template>
</xsl:stylesheet>
这会导致:
<?xml version="1.0" encoding="UTF-8"?>
<verkeersbesluiten xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<test>Hello world!</test>
<numberOfRecords>41</numberOfRecords>
<besluit>
<test2>Hello world again!</test2>
</besluit>
<besluit>
<test2>Hello world again!</test2>
</besluit>
...
</besluiten>
这看起来不错。但是当我扩展 xsl:for-each 子句时出现问题:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<verkeersbesluiten>
<test>Hello world!</test>
<numberOfRecords><xsl:value-of select="srw:searchRetrieveResponse/srw:numberOfRecords"/></numberOfRecords>
<xsl:for-each select="srw:searchRetrieveResponse/srw:records/srw:record/sru:gzd">
<besluit>
<test2>Hello world again!</test2>
</besluit>
</xsl:for-each>
</verkeersbesluiten>
</xsl:template>
</xsl:stylesheet>
这会导致:
<?xml version="1.0" encoding="UTF-8"?>
<verkeersbesluiten xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms">
<test>Hello world!</test>
<numberOfRecords>41</numberOfRecords>
</verkeersbesluiten>
我无法理解这一点。我做错了什么?
【问题讨论】:
-
我检查了这个 xml 输入文件并发现了一个警告。 “您文档中从该位置引用的架构包含错误。” 即
xsi:schemaLocation="http://www.loc.gov/zing/srw/ srw-types.xsd"和xsi:schemaLocation="http://standaarden.overheid.nl/sru gzd.xsd" -
XML 输入使用
http://www.loc.gov/zing/srw/作为默认命名空间,而select属性中的路径引用空命名空间(searchRetrieveResponse等)。您可能希望在 XSL 文件中声明命名空间 (xmlns:srw="http://www.loc.gov/zing/srw/") 并限定路径中的元素 (srw:searchRetrieveResponse等)。 -
我按照@halfbit 的建议添加了命名空间,并更改了我原来的帖子以反映这些变化。结果有所改善,但我仍然没有达到我的预期。也许您还有其他建议?