【问题标题】:XSLT Ignore namespaceXSLT 忽略命名空间
【发布时间】: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


【解决方案1】:

由于您使用的是 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 可以让它在没有警告的情况下运行,但它不会为每个部分创建一个文件,并且仍然只输出文本。这是它正在产生的内容: Title 1 USCTitle 1 Online@113-31 OLRC 2013-07-26T05:14:38 USCConverter 1.0
  • 我没有查看数据,但你确定sectiontitle 的孩子吗?这就是您的 XSLT 所说的。
  • 刚刚看了下数据。 sectiontitle 的后代。尝试将xsl:for-each 中的select 更改为chapter/section
  • @Chris - 我添加了一个完整的例子。另外,请注意我没有使用xsl:elementxsl:attributexsl:for-each。属性中的{} 是属性值模板(w3.org/TR/xslt#attribute-value-templates)。非常方便。
  • 在进行更改并在结果文档 href 属性中使用 {$href} 而不是 $href 之后,它就像一个魅力!感谢您的帮助。
猜你喜欢
  • 2018-04-30
  • 2011-12-05
  • 2015-02-18
  • 1970-01-01
  • 2015-05-01
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
相关资源
最近更新 更多