2011-10-21 15 views
5

Tôi trích xuất dữ liệu từ XML bằng XSLT 2.0. Dữ liệu có các đường dài và tôi muốn vừa với chúng thành kích thước cửa sổ bằng cách tự động ngắt dòng.Cách bọc văn bản cho vừa với cửa sổ trong XSLT

Có thể trong XSLT không?

+1

Định dạng mục tiêu của bạn (nghĩa là phương pháp xsl: output) là gì? Nếu đó là HTML hoặc XHTML thì trình duyệt sẽ đảm bảo rằng nội dung không tràn vì vậy tôi không nghĩ rằng bạn cần phải làm bất cứ điều gì. Và tất nhiên bản thân XSLT không biết gì về cửa sổ hoặc kích thước cửa sổ, nó biến đổi XML thành văn bản hoặc (X) HTML hoặc XML khác. –

+0

@MartinHonnen Đầu ra XML của tôi là văn bản chứ không phải XHTML. Có thể nếu tôi xuất nó ở định dạng văn bản? – smandape

Trả lời

6

Bạn có thể sử dụng hàm XSLT 2.0 chuẩn unparsed-text() để đọc tệp văn bản trực tiếp trong mã XSLT 2.0 của mình.

Sau đó, chỉ cần sử dụng:

replace(concat(normalize-space($text),' '), 
       '(.{0,60}) ', 
       '$1
') 

Giải thích:

này đầu tiên bình thường hóa các khoảng trắng, xóa hàng đầu và đuôi chuỗi khoảng trắng chỉ nhân vật và thay thế bất kỳ chuỗi bên trong như vậy với một không gian duy nhất.

Sau đó, kết quả của việc chuẩn hóa được sử dụng làm đối số đầu tiên cho hàm XPath 2.0 chuẩn replace().

Các mô hình phù hợp với bất kỳ (chuỗi có thể dài tối đa 61 ký tự kết thúc bằng một dấu cách.

luận

Việc thay thế quy định rằng bất kỳ chuỗi đó tìm thấy nên được thay thế bằng chuỗi trước khi vũ trụ kết thúc, nối với một . Nhân vật NL

đây là một giải pháp hoàn chỉnh, đọc và định dạng văn bản này từ tập tin C:\temp\delete\text.txt:

Dec. 13 — As always for a presidential inaugural, security and surveillance were 
extremely tight in Washington, DC, last January. But as George W. Bush prepared to 
take the oath of office, security planners installed an extra layer of protection: a 
prototype software system to detect a biological attack. The U.S. Department of 
Defense, together with regional health and emergency-planning agencies, distributed 
a special patient-query sheet to military clinics, civilian hospitals and even aid 
stations along the parade route and at the inaugural balls. Software quickly 
analyzed complaints of seven key symptoms — from rashes to sore throats — for 
patterns that might indicate the early stages of a bio-attack. There was a brief 
scare: the system noticed a surge in flulike symptoms at military clinics. 
Thankfully, tests confirmed it was just that — the flu. 

mã XSLT:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output method="text"/> 

<xsl:variable name="vText" select= 
"unparsed-text('file:///c:/temp/delete/text.txt')"/> 

<xsl:template match="/"> 
    <xsl:sequence select= 
    "replace(concat(normalize-space($vText),' '), 
      '(.{0,60}) ', 
      '$1&#xA;') 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

Kết quả là một tập hợp các đường nét, mỗi trong số đó không vượt quá chiều dài cố định là 60:

Dec. 13 — As always for a presidential inaugural, security 
and surveillance were extremely tight in Washington, DC, 
last January. But as George W. Bush prepared to take the 
oath of office, security planners installed an extra layer 
of protection: a prototype software system to detect a 
biological attack. The U.S. Department of Defense, together 
with regional health and emergency-planning agencies, 
distributed a special patient-query sheet to military 
clinics, civilian hospitals and even aid stations along the 
parade route and at the inaugural balls. Software quickly 
analyzed complaints of seven key symptoms — from rashes to 
sore throats — for patterns that might indicate the early 
stages of a bio-attack. There was a brief scare: the system 
noticed a surge in flulike symptoms at military clinics. 
Thankfully, tests confirmed it was just that — the flu. 

Cập nhật:

Trong trường hợp văn bản đến từ một tệp XML, điều này có thể được thực hiện với một thay đổi tối thiểu đối với giải pháp trên:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output method="text"/> 

<xsl:template match="/"> 
    <xsl:sequence select= 
    "replace(concat(normalize-space(text),' '), 
      '(.{0,60}) ', 
      '$1&#xA;') 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

Ở đây tôi giả sử rằng tất cả các văn bản là ở trẻ em nút văn bản duy nhất của các yếu tố đầu (tên text) của tài liệu XML:

<text> 
Dec. 13 — As always for a presidential inaugural, security and surveillance were 
extremely tight in Washington, DC, last January. But as George W. Bush prepared to 
take the oath of office, security planners installed an extra layer of protection: a 
prototype software system to detect a biological attack. The U.S. Department of 
Defense, together with regional health and emergency-planning agencies, distributed 
a special patient-query sheet to military clinics, civilian hospitals and even aid 
stations along the parade route and at the inaugural balls. Software quickly 
analyzed complaints of seven key symptoms — from rashes to sore throats — for 
patterns that might indicate the early stages of a bio-attack. There was a brief 
scare: the system noticed a surge in flulike symptoms at military clinics. 
Thankfully, tests confirmed it was just that — the flu. 
</text> 

Khi chuyển đổi này được áp dụng cho các tài liệu XML trên, kết quả tương tự như với giải pháp đầu tiên được tạo ra.

+1

+1 để có câu trả lời hữu ích. Lưu ý rằng OP đã trích xuất dữ liệu từ một tệp XML. – LarsH

+0

@LarsH: Ồ, cảm ơn, tôi bằng cách nào đó chỉ là ấn tượng ngược lại - rằng anh ấy muốn đối phó với một tệp văn bản thuần túy. Tôi sẽ thêm biến thể tệp XML vào giải pháp. –

+0

Wallah! Điều này thực sự hữu ích. Cảm ơn các bạn. – smandape

2

Tôi tưởng tượng rằng tokenize() hoặc <xsl:analyze-string> có thể được sử dụng để thực hiện điều này một cách hiệu quả, sử dụng regexp cho phép tối đa 70 ký tự và kết thúc bằng ký tự ngắt (ví dụ: dấu cách).

Để biết mã rõ ràng, hãy xem câu trả lời của XPath và XSLT tại xquery word wrap.

+2

+1 để có câu trả lời tóm tắt tốt. –

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