简体   繁体   中英

merging two paragraphs together in xml file using xslt

I have an xml file as shown below:

<?xml version='1.0' encoding='utf-8'?>
<articles>
<article id="12" title="ABC" ns-name="">Affronting discretion as do is announcing. Now months esteem oppose nearer enable too six. Absolute bachelor rendered six nay you juvenile. Vanity entire an chatty to. 

Do greatest at in learning steepest. Breakfast extremity suffering one who all otherwise suspected.  Improved so strictly produced answered elegance is. .

</article>
</articles>

My XSLT file is as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd" version="2.0">
    <!-- For defining the output of the transformation -->
    <xsl:output method="text" omit-xml-declaration="yes" indent="yes" encoding="ISO-8859-1"/>
    <xsl:strip-space elements="*" />
    <!-- Match entire root of the xml-file under consideration  -->
    <xsl:template match="article">
        <xsl:value-of select="@id"/>
        <xsl:text>&#x9;</xsl:text>
        <xsl:value-of select="@title"/>
        <xsl:text>&#x9;</xsl:text>
        <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match=
        "text()[not(string-length(normalize-space()))]"/>

    <xsl:template match=
        "text()[string-length(normalize-space()) > 0]">
        <xsl:value-of select="translate(.,'&#xA;&#xD;', '  ')"/>
    </xsl:template>

</xsl:stylesheet>

While the XSLT extracts the required information in a tab seperated form, I would like to merge two paragraphs into one.

IOW an output as shown below:

12 ABC  Affronting discretion as do is announcing. Now months esteem opposeearer enable too six. Absolute bachelor rendered six nay you juvenile. Vanity entire an chatty to. Do greatest at in learning steepest. Breakfast extremity suffering one who all otherwise suspected.  Improved so strictly produced answered elegance is.

Any pointers to achieve this would be of some help.

How about simply:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1"/>
<xsl:strip-space elements="*" />

<xsl:template match="article">
    <xsl:value-of select="@id"/>
    <xsl:text>&#9;</xsl:text>
    <xsl:value-of select="@title"/>
    <xsl:text>&#9;</xsl:text>
    <xsl:value-of select="normalize-space(.)"/>
    <!-- presumably you would want a new line for each article? -->
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Note :

  1. The entire article is a single text node; you cannot apply templates to individual parts of it.
  2. When you do <xsl:value-of select="."/> you preclude any templates to be applied to the text. You should have used xsl:apply-templates at this point in order for (one of) the other templates to have any effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM