繁体   English   中英

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

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

我有一个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>

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

XSLT以制表符分隔的形式提取所需的信息时,我想将两个段落合并为一个。

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.

任何实现这一目标的指针都会有所帮助。

简单地说:

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>

注意事项

  1. 整篇文章都是一个文本节点。 您不能将模板应用于模板的各个部分。
  2. 当您执行<xsl:value-of select="."/>将排除要应用于文本的所有模板。 此时,您应该已经使用xsl:apply-templates ,以便其他模板(其中之一)起作用。

暂无
暂无

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

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