XSLT 忽略命名空间

3
我将尝试适应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标签。

任何帮助将不胜感激。

谢谢


2
请不要过度依赖外部网站的参考资料。我们中的一些人对链接,特别是ZIP文件的链接持怀疑态度,即使我们信任它们,也会增加回答问题的工作量。此外,这些链接经常会在一段时间后消失,使得SO档案变得不太有用。 - Michael Kay
1个回答

6

由于您正在使用XSLT 2.0,因此可以在xsl:stylesheet上添加xpath-default-namespace属性。有关更多详细信息,请参见http://www.w3.org/TR/xslt20/#standard-attributes

例如:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xpath-default-namespace="http://xml.house.gov/schemas/uslm/1.0">

您还可以选择使用*作为路径中每个元素的前缀。但是,如果您的样式表变得越来越复杂,这可能需要花费很多时间。

示例:

ancestor::*:title/*:num

完整示例:

完整示例:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xpath-default-namespace="http://xml.house.gov/schemas/uslm/1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>

    <xsl:template match="section">
        <xsl:result-document href="{ancestor::title/num/@value}-{ancestor::chapter/num/@value}-{num/@value}.xml">
            <structure>
                <unit label="title" identifier="{ancestor::title/num/@value}" 
                    order_by="{ancestor::title/num/@value}" level="1">
                    <xsl:value-of select="concat(ancestor::title/num,' ',ancestor::title/heading)"/>
                </unit>
            </structure>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

添加xpath-default-namespace可以使其运行而不出现警告,但它并不会为每个部分创建一个文件,仍然只输出文本。这是它正在生成的内容: 标题1 美国法典标题 1 在线@113-31 OLRC 2013-07-26T05:14:38 USC转换器1.0 - Chris
我还没有查看数据,但你确定sectiontitle的子元素吗?这是你的XSLT所说的。 - Daniel Haley
刚刚查看了数据。sectiontitle 的后代。尝试将 xsl:for-each 中的 select 更改为 chapter/section - Daniel Haley
@Chris - 我添加了一个完整的示例。另外,请注意我没有使用 xsl:elementxsl:attributexsl:for-each。属性中的 {} 是属性值模板 (http://www.w3.org/TR/xslt#attribute-value-templates)。非常方便。 - Daniel Haley
在进行了那个更改并且在结果文档中的 href 属性中使用 {$href} 而不仅仅是 $href 之后,它完美地运行了!感谢您的帮助。 - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接