在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 acceptNone
nor""
as namespace prefix. I've heard about_namespace_map
in thelxml.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).
原文:
最满意答案
你在找什么是
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;
相关问答
更多-
C++中这句代码是什么意思? ofstream print(ofstream);[2021-07-13]
这个不就是定义一个函数吗? ofstream 是标准文件输出流,定义函数的时候参数是可以不写的,可以只写参数类型。 C++ primary 是本好书,上面有记载ofstream -
有没有一种简单的方法来获取用C ++打印的字符数?(Is there a simple way to get the number of characters printed in C++?)[2023-11-27]
您可以将自己的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() << ... -
为什么std :: ofstream会添加额外的#13(换行符)字符?(Why does std::ofstream add extra #13 (newline) characters?)[2022-10-04]
这些流默认为文本模式 ,这意味着在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 ... -
C ++找到ofstream的大小(C++ find size of ofstream)[2022-07-27]
你想要。 这可用于输出流(例如,ostream,ofstream,stringstream)。 有一个匹配的tellg可用于输入流(例如,istream,ifstream,stringstream)。 请注意, stringstream支持输入和输出,所以它既有tellg也有tellg 。 至于保持两条直线, p表示put和g表示get ,所以如果你想要“get position”(即读取位置),你使用tellg 。 如果你想put (写)位置,你使用tellp 。 请注意, fstream支持输入和输出, ... -
c ++:ofstream问题(c++ : ofstream issues)[2023-09-01]
问题在于您多次打开和关闭文件,每次打开文件时都会销毁之前存在的内容。 可能你应该只在构造函数中打开一次文件(并且不要在那里关闭文件)。 另一种方法是以'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 ...
-
ofstream错误(ofstream error)[2023-11-24]
范围问题。 你在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"); ...