XSLT忽略名称空间
我试图习惯XSLT,我理解名称空间的原因,但我只是尝试将本地XML文件转换为本地应用程序使用 我正在尝试转换在此处找到的文件: 使用此代码:XSLT忽略名称空间,xslt,xml-namespaces,Xslt,Xml Namespaces,我试图习惯XSLT,我理解名称空间的原因,但我只是尝试将本地XML文件转换为本地应用程序使用 我正在尝试转换在此处找到的文件: 使用此代码: <?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"
<?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标记
任何帮助都将不胜感激
谢谢因为您使用的是XSLT2.0,所以可以在
xsl:stylesheet
上添加xpath默认名称空间
属性。有关更多详细信息,请参阅
例如:
<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: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默认名称空间可以让它在没有警告的情况下运行,但它不会为每个部分创建文件,仍然只是输出文本。以下是它正在制作的内容:标题1Online@113-31 OLRC 2013-07-26T05:14:38 USC Converter 1.0我还没有查看数据,但是您确定部分
是标题
的子项吗?这就是您的XSLT所说的。只需查看数据<代码>部分是标题
的后代。尝试将xsl:for each
中的select
更改为chapter/section
。@Chris-I添加了一个完整的示例。另外,请注意,我没有为每个使用xsl:element
、xsl:attribute
或xsl:for-each
。属性中的{}
是属性值模板()。非常方便。在做出更改并使用{$href}而不是结果文档href属性中的$href之后,它就像一个符咒!谢谢你的帮助。请不要太依赖于对外部网站的引用。我们中的一些人不信任链接,尤其是ZIP文件的链接,即使我们信任它们,也会增加回答问题的难度。此外,链接往往会及时消失,使SO档案变得不那么有用。
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>