使用XSLT删除多余的空行,而不使用缩进

时间:2015-10-28 19:01:28

标签: xml xslt

我正在为InDesign导入转换XML文档,这导致输出文件中出现了许多空白行。这些空白行将转换为InDesign中的空段落,因此必须移动。我试过用了 <xsl:strip-space elements="*"/>但会删除所有换行符(在InDesign中生成一个段落)。将xsl:output设置为indent可以解决此问题,但在InDesign导入过程中缩进会延续,并且还会阻塞应嵌套在一行上的某些混合内容节点。

为了说明,我的输出文件应仅删除空行,没有缩进:

<wrapper>
<h1>Header</h1>
<para>text <italic>text</italic> text></p>
<blockquote>
<p>here is a block quote please don't indent me</p>
</blockquote>
<para>more text more text yay</para>
<wrapper>

有什么想法吗?我玩了normalize-space并翻译命令来替换换行符(&#xA;),但我没有运气。我是一名XSLT爱好者,所以我很感激任何帮助。

修改 我添加了其他详细信息和示例代码。请善待,我最好是业余爱好者!

下面是转换前的XML文件示例。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE book-part-wrapper PUBLIC "-//NLM//DTD BITS Book Interchange DTD v1.0 20131225//EN" "http://jats.nlm.nih.gov/extensions/bits/1.0/BITS-book1.dtd">
<book-part-wrapper dtd-version="1.0" xml:lang="en" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<book-meta>
</book-meta>
<book-part book-part-type="chapter" seq="10">
<book-part-meta>
<title-group>
<title content-type="bookpart-title">Test Title</title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Smith</surname>
<given-names>John</given-names>
</name>
</contrib>
<contrib contrib-type="author">
<name>
<surname>Jones</surname>
<given-names>Jane</given-names>
</name>
</contrib>
</contrib-group>
</book-part-meta>
<body>
<sec>
<title content-type="head-a">Header 1</title>
<p content-type="p-first"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis sem, suscipit at sodales eget, faucibus dignissim tellus. Curabitur dictum pulvinar lectus, sit amet ornare ex ultricies vel.<xref ref-type="fn" rid="fn1">1</xref> Suspendisse turpis sem, blandit ut elit eu, pharetra vehicula sem. Suspendisse tincidunt enim at magna auctor lobortis. Aenean egestas ligula purus, non vulputate ipsum porttitor sed. Quisque a maximus magna, eget pellentesque odio. Vivamus porttitor massa ut posuere euismod. Donec vehicula mi non libero dapibus semper. Fusce nec felis vel nulla auctor volutpat. Fusce pellentesque pellentesque nunc ac blandit. Fusce sed erat feugiat massa blandit vehicula.</p> 
</sec>
</body>
<back>
<fn-group>
<title content-type="head-notes">Notes</title>
<fn id="fn1"><label>1</label><p>Jeff White, &#8220;<italic>Book Title</italic>.&#8221; N.D. Accessed January 27, 2013</p></fn>
</fn-group>
</back>
</book-part>
</book-part-wrapper>

以下是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"    
    xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" 
    xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" 
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" 
    xmlns:mml="http://www.w3.org/1998/Math/MathML" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:xi="http://www.w3.org/2001/XInclude"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output encoding="UTF-8" indent="no" method="xml" omit-xml-declaration="no" />        


    <!-- Identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="book-part"/>
        </xsl:copy>
    </xsl:template>                       

    <!-- Skip these nodes -->
    <xsl:template match="book-meta|book-part-id"/>    

    <xsl:template match="book-part">
        <xsl:copy>                        
            <xsl:apply-templates select="@*"/>            
            <xsl:apply-templates select=".//book-part-id[@book-part-id-type='doi']"/>
            <xsl:apply-templates select=".//title[@content-type='bookpart-title']"/>            
            <body>                
                <xsl:apply-templates select=".//title[@content-type='bookpart-title']"/>                
                <xsl:apply-templates select=".//body"/>
                <xsl:apply-templates select=".//fn-group"/>
            </body>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="body|label|p|title|fn|italic">                
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>        
        </xsl:copy>
    </xsl:template>

    <!-- Apply all child nodes; don't copy the element itself -->
    <xsl:template match="book-part-meta|title-group|disp-quote|sec|contrib-group|fn-group">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

这是我的输出:

<?xml version="1.0" encoding="UTF-8"?><book-part-wrapper xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" dtd-version="1.0" xml:lang="en"><book-part book-part-type="chapter" seq="10"><title content-type="bookpart-title">Test Title</title><body xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xs="http://www.w3.org/2001/XMLSchema"><title content-type="bookpart-title">Test Title</title><body>

<title content-type="head-a">Header 1</title>
<p content-type="p-first"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis sem, suscipit at sodales eget, faucibus dignissim tellus. Curabitur dictum pulvinar lectus, sit amet ornare ex ultricies vel.<xref ref-type="fn" rid="fn1"/> Suspendisse turpis sem, blandit ut elit eu, pharetra vehicula sem. Suspendisse tincidunt enim at magna auctor lobortis. Aenean egestas ligula purus, non vulputate ipsum porttitor sed. Quisque a maximus magna, eget pellentesque odio. Vivamus porttitor massa ut posuere euismod. Donec vehicula mi non libero dapibus semper. Fusce nec felis vel nulla auctor volutpat. Fusce pellentesque pellentesque nunc ac blandit. Fusce sed erat feugiat massa blandit vehicula.</p> 

</body>
<title content-type="head-notes">Notes</title>
<fn id="fn1"><label>1</label><p>Jeff White, <italic>Book Title</italic>. N.D. Accessed January 27, 2013</p></fn>
</body></book-part></book-part-wrapper>

如果我将<xsl:strip-space elements="*"/>添加到XSLT文件中,我的输出将全部返回到一行:

<?xml version="1.0" encoding="UTF-8"?><book-part-wrapper xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" dtd-version="1.0" xml:lang="en"><book-part book-part-type="chapter" seq="10"><title content-type="bookpart-title">Test Title</title><body xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xs="http://www.w3.org/2001/XMLSchema"><title content-type="bookpart-title">Test Title</title><body><title content-type="head-a">Header 1</title><p content-type="p-first"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed felis sem, suscipit at sodales eget, faucibus dignissim tellus. Curabitur dictum pulvinar lectus, sit amet ornare ex ultricies vel.<xref ref-type="fn" rid="fn1"/> Suspendisse turpis sem, blandit ut elit eu, pharetra vehicula sem. Suspendisse tincidunt enim at magna auctor lobortis. Aenean egestas ligula purus, non vulputate ipsum porttitor sed. Quisque a maximus magna, eget pellentesque odio. Vivamus porttitor massa ut posuere euismod. Donec vehicula mi non libero dapibus semper. Fusce nec felis vel nulla auctor volutpat. Fusce pellentesque pellentesque nunc ac blandit. Fusce sed erat feugiat massa blandit vehicula.</p></body><title content-type="head-notes">Notes</title><fn id="fn1"><label>1</label><p>Jeff White, <italic>Book Title</italic>. N.D. Accessed January 27, 2013</p></fn></body></book-part></book-part-wrapper>

0 个答案:

没有答案