xml - xslt 问题(命名空间?)

标签 xml xslt xhtml namespaces

我有一个这样的文件:

<?xml-stylesheet type="text/css" href="http://ltw1001.web.cs.unibo.it/svg.css" encoding="UTF-8"?>
<!DOCTYPE html  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg= "http://www.w3.org/2000/svg">
<body>
<svg:svg width="500" height="560" version="1.1" >

...
...

</svg:svg></body></html>

我应该只提取我尝试过的正文内容:
<?xml version="1.0" standalone="no"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/HTML/1998/html4">
    <xsl:template match="/">
        <xsl:value-of select="//body" />
    </xsl:template>
</xsl:stylesheet>

但它不起作用

最佳答案

您(至少)有两个问题:

  • 默认 namespace 不同,因此 XSL 中的模板匹配将不起作用。使它们匹配或在样式表中提供显式命名空间前缀。
  • value-of-select 将返回 body 元素的文本值,这可能不是您想要的。

  • 如果您想要完成的只是将 SVG 部分输出为 SVG 文档类型,请执行以下操作:
  • 谷歌“XSL Identity Transform”以了解如何从输入到输出进行“深度复制”。
  • 添加 <xsl:output ...>标签 doctype-publicdoctype-system指定要输出的文档类型信息的属性。

  • 这是未经测试的,但应该非常接近。您必须添加文档类型信息:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet 
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:svg= "http://www.w3.org/2000/svg"
      version="2.0">
    
      <xsl:output method="xml" doctype-public="..." doctype-system="..."/>
    
      <xsl:template match="/">
        <xsl:apply-templates select="//svg:svg"/>    
      </xsl:template>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    关于xml - xslt 问题(命名空间?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3151472/

    相关文章:

    javascript - 如何使用 jQuery 获取该元素的索引?

    c# - 获得不同的值(value);网络

    java - 在 Java 中使用 Transformer 实现 XSLT 的多个输出?

    javascript - 脚本标记中何时需要 CDATA 部分?

    xml - 如何以编程方式完全删除 Flex 中的属性?

    java - float 操作按钮加号未显示

    xml - XSLT 在不同元素中设置来自另一个 XML 文件的属性值

    java - 更改以前的 ListView 中的 ListView 项目

    javascript - XSLT 不适用于 IE 11,不会转换 xml

    css - 如果我使用 em 来调整文本大小,我是否也应该在 em 中定义宽度、高度、边距、填充、行高?