2011-10-21 20 views
5

में विंडो फिट करने के लिए पाठ को कैसे लपेटें I एक्सएसएलटी 2.0 का उपयोग कर एक्सएमएल से डेटा निकालने जा रहा हूं। डेटा में लंबी लाइनें हैं और मैं उन्हें लाइनों को स्वचालित रूप से तोड़कर विंडो आकार में फिट करना चाहता हूं।एक्सएसएलटी

क्या यह एक्सएसएलटी में संभव है?

+1

आपका लक्ष्य प्रारूप क्या है (यानी xsl: आउटपुट विधि)? यदि वह HTML या XHTML है तो ब्राउज़र यह सुनिश्चित करेगा कि सामग्री अधिक नहीं हो पाती है, इसलिए मुझे नहीं लगता कि आपको कुछ भी करने की आवश्यकता है। और निश्चित रूप से एक्सएसएलटी स्वयं खिड़कियों या खिड़की के आकार के बारे में कुछ भी नहीं जानता है, यह एक्सएमएल को टेक्स्ट या (एक्स) एचटीएमएल या अन्य एक्सएमएल में बदल देता है। –

+0

@MartinHonnen मेरा एक्सएमएल आउटपुट टेक्स्ट है और एक्सएचटीएमएल नहीं है। क्या यह संभव है यदि मैं इसे टेक्स्ट प्रारूप में आउटपुट करता हूं? एक अच्छा संक्षेप उत्तर के लिए – smandape

उत्तर

6

आप अपने एक्सएसएलटी 2.0 कोड में सीधे टेक्स्ट फ़ाइल पढ़ने के लिए मानक एक्सएसएलटी 2.0 फ़ंक्शन unparsed-text() का उपयोग कर सकते हैं।

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

स्पष्टीकरण:

तो बस का उपयोग

यह पहली को सामान्य सफेद स्थान, अग्रणी को हटाने और रिक्त स्थान को केवल-वर्णों के क्रम को अनुगामी और के साथ किसी भी तरह के भीतरी अनुक्रम की जगह एक ही जगह

फिर सामान्यीकरण का परिणाम मानक XPath 2.0 फ़ंक्शन replace() के पहले तर्क के रूप में उपयोग किया जाता है।

मेल खाने वाला पैटर्न अधिकतम 61 वर्णों के किसी भी (सबसे लंबे समय तक संभव अनुक्रम है कि एक स्थान के साथ समाप्त होता है।

प्रतिस्थापन तर्क यह बताता है कि ऐसे किसी भी पाया अनुक्रम को समाप्त होने वाले अंतरिक्ष से पहले स्ट्रिंग से प्रतिस्थापित किया जाना चाहिए एक साथ concatenated । एनएल चरित्र

यहाँ, एक पूर्ण समाधान है पढ़ने और फ़ाइल 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. 

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> 

परिणाम, लाइनों का एक सेट है, जिनमें से प्रत्येक 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. 

अद्यतन:

मामले में पाठ एक एक्सएमएल फ़ाइल से आता है, यह उपर्युक्त समाधान में न्यूनतम परिवर्तन के साथ किया जा सकता है:

<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> 

यहाँ मुझे लगता है कि सभी पाठ XML दस्तावेज़ के शीर्ष तत्व (नाम text) का केवल पाठ नोड बच्चे में है:

<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> 

इस बदलाव से ऊपर XML दस्तावेज़ पर लागू किया जाता है, पहले समाधान के साथ ही वही परिणाम उत्पन्न होता है।

+1

+1। ध्यान दें कि ओपी एक एक्सएमएल फ़ाइल से डेटा निकाल रहा था। – LarsH

+0

@ लार्सएच: ओह, धन्यवाद, मैंने किसी भी तरह से विपरीत प्रभाव था - कि वह एक सादे पाठ फ़ाइल से निपटना चाहता था। मैं समाधान में एक्सएमएल फ़ाइल संस्करण जोड़ दूंगा। –

+0

वालाह! यह वास्तव में मदद करता है। आप लोगों को धन्यवाद। – smandape

2

मैं कल्पना कर सकते हैं कि tokenize() या <xsl:analyze-string> इस कुशलता से करने के लिए, एक regexp कि (माना) 70 अक्षरों की अनुमति देता है, और एक को तोड़ने चरित्र (जैसे अंतरिक्ष) के साथ समाप्त होता का उपयोग कर किया जा सकता है।

स्पष्ट कोड के लिए, xquery word wrap पर XPath और XSLT उत्तरों देखें।

+2

+1। सहायक उत्तर के लिए –