首页 \ 问答 \ 在lxml中定义默认命名空间(未加前缀)(Define default namespace (unprefixed) in lxml)

在lxml中定义默认命名空间(未加前缀)(Define default namespace (unprefixed) in lxml)

当用lxml渲染XHTML时,一切都很好,除非你碰巧使用Firefox,它似乎无法处理名称空间前缀的XHTML元素和javascript。 虽然Opera能够执行javascript(这同样适用于jQuery和MathJax),但无论XHTML命名空间是否有前缀(在我的情况下是h: ),在Firefox中,脚本都会因奇怪的错误而中止( this.head在MathJax的情况下, this.head是未定义的)。

我知道register_namespace函数,但它既不接受None也不接受""作为名称空间前缀。 我在lxml.etree模块中听说过_namespace_map ,但是我的Python抱怨这个属性不存在(版本问题?)

是否还有其他方法可以删除XHTML命名空间的名称空间前缀? 请注意, str.replace ,如在另一个相关问题的答案中所建议的, 不是我可以接受的方法,因为它不了解XML语义并且可能很容易搞砸了生成的文档。

根据要求,您将找到两个可供使用的示例。 一个具有名称空间前缀一个没有 。 第一个将在Firefox中显示0(错误),第二个将显示1(正确)。 Opera将使两者都正确。 这显然是一个Firefox错误 ,但这只是作为一个理由,希望无前缀XHTML与lxml - 还有其他很好的理由,以减少移动客户端的流量等(甚至h:如果你考虑数十或hndret的html是相当多的标签)。


When rendering XHTML with lxml, everything is fine, unless you happen to use Firefox, which seems unable to deal with namespace-prefixed XHTML elements and javascript. While Opera is able to execute the javascript (this applies to both jQuery and MathJax) fine, no matter whether the XHTML namespace has a prefix (h: in my case) or not, in Firefox the scripts will abort with weird errors (this.head is undefined in the case of MathJax).

I know about the register_namespace function, but it does neither accept None nor "" as namespace prefix. I've heard about _namespace_map in the lxml.etree module, but my Python complains that this attribute doesn't exist (version issues?)

Is there any other way removing the namespace prefix for the XHTML namespace? Note that str.replace, as suggested in the answer to another, related question, is not a method I could accept, as it is not aware of XML semantics and might easily screw up the resulting document.

As per request, you'll find two examples ready to use. One with namespace prefixes and one without. The first one will display 0 in Firefox (wrong) and the second one will display 1 (correct). Opera will render both correct. This is obviously a Firefox bug, but this only serves as a rationale for wanting prefixless XHTML with lxml – there are other good reasons as to reduce traffic for mobile clients etc (even h: is quite a lot if you consider tens or hundret of html tags).


原文:
更新时间:2021-11-29 14:11

最满意答案

你在找什么是tellp()

你可以像这样使用它:

ofstream file("outputFile");

auto pos1 = file.tellp();
file << "hello";
auto pos2 = file.tellp();
std::cout << pos2 - pos1 << std::endl;

What you're looking for is tellp().

You could use it like so:

ofstream file("outputFile");

auto pos1 = file.tellp();
file << "hello";
auto pos2 = file.tellp();
std::cout << pos2 - pos1 << std::endl;

相关问答

更多
  • 这个不就是定义一个函数吗? ofstream 是标准文件输出流,定义函数的时候参数是可以不写的,可以只写参数类型。 C++ primary 是本好书,上面有记载ofstream
  • 您可以将自己的streambuf关联到cout来计算字符数。 这是包装它的一类: class CCountChars { public: CCountChars(ostream &s1) : m_s1(s1), m_buf(s1.rdbuf()), m_s1OrigBuf(s1.rdbuf(&m_buf)) {} ~CCountChars() { m_s1.rdbuf(m_s1OrigBuf); m_s1 << endl << "output " << m_buf.GetCount() << ...
  • 这些流默认为文本模式 ,这意味着在Windows中,如果您编写\n那么文件将会被\r\n 。 因此,如果您写入\r\n那么文件将被\r\r\n 。 要解决这个问题,可以在代码中写入\n ; 或以二进制模式打开文件: auto output_stream = std::ofstream(output_file.c_str(), std::ios::binary); The streams default to text mode, which means that in Windows, if you wr ...
  • 你想要。 这可用于输出流(例如,ostream,ofstream,stringstream)。 有一个匹配的tellg可用于输入流(例如,istream,ifstream,stringstream)。 请注意, stringstream支持输入和输出,所以它既有tellg也有tellg 。 至于保持两条直线, p表示put和g表示get ,所以如果你想要“get position”(即读取位置),你使用tellg 。 如果你想put (写)位置,你使用tellp 。 请注意, fstream支持输入和输出, ...
  • 问题在于您多次打开和关闭文件,每次打开文件时都会销毁之前存在的内容。 可能你应该只在构造函数中打开一次文件(并且不要在那里关闭文件)。 另一种方法是以'append'模式打开文件,但这样效率很低,打开文件是一项昂贵的操作。 正如Liho所说 ofsFile.open("/home/user/example.txt", std::ofstream::out | std::ofstream::app); The problem is that you open and close the file multi ...
  • 这里的问题是你存储的是ofstream值而不是reference。 您可以通过更改ANTPred (以及类似地更改ATPred)来使代码工作 ,如下所示: class ANTPred { private: std::ofstream &outputFile; public: ANTPred(std::ofstream &file) : outputFile(file) { // other construction work } }; 虽然我说这将使您的代码工作,但 ...
  • 我敢打赌,该byte是char或其中的一些变体。 在这种情况下,您将_b设置为具有代码20的字符,其中ASCII是控制字符。 流输出将尝试输出字符而不是数字。 如果要获取数字,可以将其转换为另一种整数类型: p_file << static_cast(_b); I bet that byte is char or some variant thereof. In that case you are setting _b to the character with code 20, which i ...
  • 你不能复制或分配std::ofstream ,这意味着你不能这样做: file = openedFile; 您需要正确初始化它,或者移动 - 复制 - 分配。 初始化(首选): FileWriter(string fileName) : file("data.bin", ios::out | ios::binary) { ... } 移动复制分配: file = std::move(openedFile); 或者,您可以使用std::ofstream::open方法: file.open("da ...
  • 范围问题。 你在while循环中声明了outfile,但是试图在while循环之外访问它。 移动ofstream outfile("encrypted.txt", ios::out); 到ifstream infile(filename.c_str());后面的行ifstream infile(filename.c_str()); 这是在你的while(!infile.eof()) 。 Scoping issue. You are declaring outfile within your while lo ...
  • 你在找什么是tellp() 。 你可以像这样使用它: ofstream file("outputFile"); auto pos1 = file.tellp(); file << "hello"; auto pos2 = file.tellp(); std::cout << pos2 - pos1 << std::endl; What you're looking for is tellp(). You could use it like so: ofstream file("outputFile"); ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。