简体   繁体   English

使用xslt将两个段落合并到xml文件中

[英]merging two paragraphs together in xml file using xslt

I have an xml file as shown below: 我有一个xml文件,如下所示:

<?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: 我的XSLT文件如下所示:

<?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. XSLT以制表符分隔的形式提取所需的信息时,我想将两个段落合并为一个。

IOW an output as shown below: IOW的输出如下所示:

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 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. 当您执行<xsl:value-of select="."/>将排除要应用于文本的所有模板。 You should have used xsl:apply-templates at this point in order for (one of) the other templates to have any effect. 此时,您应该已经使用xsl:apply-templates ,以便其他模板(其中之一)起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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