2008-09-26 39 views
21

Tôi cần chuyển đổi tài liệu HTML thành XML hợp lệ, tốt nhất là XHTML. Cách tốt nhất để làm điều này là gì? Có ai biết một bộ công cụ/thư viện/mẫu/... bất cứ điều gì giúp tôi để có được công việc đó thực hiện?Làm thế nào để chuyển đổi HTML sang XHTML?

Để rõ ràng hơn một chút ở đây, ứng dụng của tôi phải tự động chuyển đổi khi chạy. Tôi không tìm kiếm một công cụ giúp tôi di chuyển một số trang sang XHTML theo cách thủ công.

Trả lời

23

Convert from HTML to XML with HTML Tidy

Downloadable Binaries

JRoppert, Đối với nhu cầu của bạn, tôi đoán bạn có thể muốn nhìn vào Sources

c:\temp>tidy -help 
tidy [option...] [file...] [option...] [file...] 
Utility to clean up and pretty print HTML/XHTML/XML 
see http://tidy.sourceforge.net/ 

Options for HTML Tidy for Windows released on 14 February 2006: 

File manipulation 
----------------- 
-output <file>, -o write output to the specified <file> 
<file> 
-config <file>  set configuration options from the specified <file> 
-file <file>, -f write errors to the specified <file> 
<file> 
-modify, -m   modify the original input files 

Processing directives 
--------------------- 
-indent, -i   indent element content 
-wrap <column>, -w wrap text at the specified <column>. 0 is assumed if 
<column>   <column> is missing. When this option is omitted, the 
        default of the configuration option "wrap" applies. 
-upper, -u   force tags to upper case 
-clean, -c   replace FONT, NOBR and CENTER tags by CSS 
-bare, -b   strip out smart quotes and em dashes, etc. 
-numeric, -n  output numeric rather than named entities 
-errors, -e   only show errors 
-quiet, -q   suppress nonessential output 
-omit    omit optional end tags 
-xml    specify the input is well formed XML 
-asxml, -asxhtml convert HTML to well formed XHTML 
-ashtml    force XHTML to well formed HTML 
-access <level>  do additional accessibility checks (<level> = 0, 1, 2, 3). 
        0 is assumed if <level> is missing. 

Character encodings 
------------------- 
-raw    output values above 127 without conversion to entities 
-ascii    use ISO-8859-1 for input, US-ASCII for output 
-latin0    use ISO-8859-15 for input, US-ASCII for output 
-latin1    use ISO-8859-1 for both input and output 
-iso2022   use ISO-2022 for both input and output 
-utf8    use UTF-8 for both input and output 
-mac    use MacRoman for input, US-ASCII for output 
-win1252   use Windows-1252 for input, US-ASCII for output 
-ibm858    use IBM-858 (CP850+Euro) for input, US-ASCII for output 
-utf16le   use UTF-16LE for both input and output 
-utf16be   use UTF-16BE for both input and output 
-utf16    use UTF-16 for both input and output 
-big5    use Big5 for both input and output 
-shiftjis   use Shift_JIS for both input and output 
-language <lang> set the two-letter language code <lang> (for future use) 

Miscellaneous 
------------- 
-version, -v  show the version of Tidy 
-help, -h, -?  list the command line options 
-xml-help   list the command line options in XML format 
-help-config  list all configuration options 
-xml-config   list all configuration options in XML format 
-show-config  list the current configuration settings 

Use --blah blarg for any configuration option "blah" with argument "blarg" 

Input/Output default to stdin/stdout respectively 
Single letter options apart from -f may be combined 
as in: tidy -f errs.txt -imu foo.html 
For further info on HTML see http://www.w3.org/MarkUp 
0

Cách đơn giản nhất là để thiết lập Visual Studio IDE của bạn để xác định thay đổi bạn cần thực hiện. Bạn có thể thực hiện việc này trong Visual Studio 2008 bằng cách: Công cụ, Tùy chọn, Trình chỉnh sửa văn bản, HTML, Xác thực và chọn mục tiêu phù hợp. Có thể là XHTML 1.1 hoặc XHTML 1.0 Chuyển tiếp.

Đối với một số thông tin về các loại khác nhau, đọc: http://msdn.microsoft.com/en-us/library/aa479043.aspx

Sau đó, bạn cần phải làm việc thông qua các điểm đánh dấu trên trang của bạn.

+0

Xin lỗi, tôi không rõ ràng trong câu hỏi của mình. Tôi cần phải thực hiện chuyển đổi tự động trong thời gian chạy. – JRoppert

4

Validator.nu HTML Parser đi kèm với chương trình mẫu HTML2XML thực hiện chuyển đổi bằng cách sử dụng thuật toán phân tích cú pháp HTML5 và quy tắc cưỡng chế infoset.

4

Sử dụng Html2Xhtml cho .NET 4.0:

Trong bộ nhớ chuỗi-to-string chuyển đổi:

var xhtml = Html2Xhtml.RunAsFilter(stdin => stdin.Write(html)).ReadToEnd(); 

Trong bộ nhớ chuỗi-to-XDocument chuyển đổi:

var xdoc = Html2Xhtml.RunAsFilter(stdin => stdin.Write(html)).ReadToXDocument(); 

Xem http://corsis.sourceforge.net/index.php/Html2Xhtml để biết thêm thông tin.

+0

Tôi đã có cùng một câu hỏi và sử dụng câu trả lời này, nó hoạt động rất đẹp. Đặc biệt là để chuyển đổi sang XElement. – Beaker

2

http://corsis.sourceforge.net/index.php/Html2Xhtml http://corsis.sourceforge.net/index.php/Html2Xhtml

Html2Xhtml là một thư viện .NET 4.0 để chuyển đổi HTML sang XHTML cấp phép theo GPLv2 hoặc cao hơn.

Tôi đã thử nghiệm Html2Xhtml trong quá trình xây dựng lại cục bộ cơ sở dữ liệu trực tuyến lớn của Liên minh châu Âu. Tidy/Tidy.NET thậm chí sẽ không tạo ra đầu ra hợp lệ phần lớn thời gian, HTML-to-XML của Chilkat chậm một chút và tạo ra các kết quả lạ (các phần tử thất lạc, thiếu, không giải thích được). Trong nỗ lực tìm một công cụ chuyển đổi miễn phí, nhanh chóng và đáng tin cậy, tôi đã tạo thư viện này. Nó chuyển đổi 2 - 4x nhanh hơn tất cả các thư viện khác mà tôi đã kiểm tra.

Html2Xhtml, kết hợp với sức mạnh của LINQ to XML, là một công cụ tuyệt vời cho tất cả các trường hợp thu thập dữ liệu quy mô lớn và các tình huống thu thập dữ liệu web.

2

bạn có thể chuyển đổi html để XHTML với tập tin thực thi gọn gàng:

gọn gàng -asxhtml -numeric < chỉ mục.html> index.xhml

bạn có thể kiểm tra triển khai C# here.

Các vấn đề liên quan