【发布时间】:2013-09-10 20:58:53
【问题描述】:
我正在尝试习惯 XSLT,并且我理解命名空间的原因,但我只是尝试将本地 XML 文件转换为由本地应用程序使用。
我正在尝试转换此处找到的文件:http://uscodebeta.house.gov/download/releasepoints/us/pl/113/31/xml_usc01@113-31.zip
使用此代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" name="xml"/>
<xsl:template match="//title">
<xsl:for-each select="section">
<xsl:variable name="href"><xsl:value-of select="ancestor::title/num/@value" />-<xsl:value-of select="ancestor::chapter/num/@value" />-<xsl:value-of select="num/@value" />.xml</xsl:variable>
<xsl:result-document href="$href">
<xsl:element name="structure">
<xsl:element name="unit">
<xsl:attribute name="label">title</xsl:attribute>
<xsl:attribute name="identifier">
<xsl:value-of select="ancestor::title/num/@value" />
</xsl:attribute>
<xsl:attribute name="order_by">
<xsl:value-of select="ancestor::title/num/@value" />
</xsl:attribute>
<xsl:attribute name="level">1</xsl:attribute>
<xsl:value-of select="ancestor::title/num" /> <xsl:value-of select="ancestor::title/heading"/>
</xsl:element>
</xsl:element>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
进入此处找到的 XML 示例: https://github.com/statedecoded/statedecoded/wiki/XML-Format-for-Parser
这只是第一个元素的转换,但是在命令行上使用 Saxon 运行时,我收到了警告:
Warning: SXXP0005: The source document is in namespace http://xml.house.gov/schemas/uslm/1.0, but all the template rules match elements in no namespace
并且输出是纯文本而不是 XML 标记。
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
请不要过于依赖对外部网站的引用。我们中的一些人不信任链接,尤其是指向 ZIP 文件的链接,即使我们信任它们,它也会增加回答问题的努力。此外,链接通常会及时消失,从而降低 SO 存档的用处。
标签: xslt xml-namespaces