2015-03-31 40 views
1

在同一行出現,我有一些XML這看起來是這樣的:節點的所有的孩子都在輸出

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE article PUBLIC "-//NLM//DTD Journal Publishing DTD v2.3 20070202//EN" 
"http://dtd.nlm.nih.gov/publishing/2.3/journalpublishing.dtd"><article> 
<front> 
<!-- Some metadata --> 
</front> 
<body> 
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel metus felis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas <italic>habitasse</italic> venenatis.</p> 
<!-- More elements --> 
</body> 
<back> 
<ref-list> 
<ref id="id-28299"><nlm-citation citation-type="journal"> 
<person-group person-group-type="author"><name><surname>Bar</surname> 
<given-names>F</given-names> 
</name> 
</person-group> 
<article-title>Baz</article-title> 
<source>Made up</source> 
<year>1970</year> 
<volume>27</volume> 
<issue>(4)</issue> 
<fpage>3</fpage> 
<lpage>7</lpage> 
<pub-id pub-id-type="doi">http://some.url.org</pub-id> 
</nlm-citation> 
</ref> 
<!-- More ref entries --> 
</ref-list> 
</back> 
</article> 

我需要改變它,以便ref元素(和 ref元素,所有其他東西都必須保持原樣)每個都包含在單個行中,元素之間有一個空格。 I.E.

<ref id="id-28299"><nlm-citation citation-type="journal"> <person-group person-group-type="author"><name><surname>Bar</surname> <given-names>F</given-names></name></person-group> <article-title>Baz</article-title> <source>Made up</source> <year>1970</year> <volume>27</volume> <issue>(4)</issue> <fpage>3</fpage> <lpage>7</lpage> <pub-id pub-id-type="doi">http://some.url.org</pub-id></nlm-citation></ref> 

我可以展開使用這個樣式表的元素做到這一點:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink"> 

<xsl:output method="xml" indent="yes"/> 

<xsl:template match="*"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="ref//*"> 
    <xsl:apply-templates/><xsl:if test="following-sibling::*"><xsl:text> </xsl:text></xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

但我想保持標籤。所以我嘗試了以下內容:

<xsl:template match="ref//*"> 
    <xsl:element name="{local-name()}"><xsl:apply-templates/></xsl:element><xsl:if test="following-sibling::*"><xsl:text> </xsl:text></xsl:if> 
</xsl:template> 

但是現在每個元素都會出現在不同的行上。我也嘗試過使用<xsl:strip-space>,但這似乎沒有改變輸出。

+0

爲什麼這很重要?您正在輸出XML。 – 2015-03-31 11:46:22

+0

您是否嘗試將縮進設置爲「no」....''。您還需要執行'',否則只會複製空白節點。 – 2015-03-31 11:46:53

+0

這很重要,因爲只有空白節點纔會保留,並在導入Adobe InDesign時產生問題。計劃是將這種轉變應用於進口。我嘗試過'indent =「no」',但似乎把* everything *放在一行上,這不是我們想要的。 – jorbas 2015-03-31 13:26:34

回答

1

你可以嘗試什麼是設置了「縮進」選項和「無」,然後而是採用strip-space,這將輸入XML移除所有的空格,有一個特定的模板忽略ref下空白僅節點。這將阻止它們由默認模板輸出。

試試這個XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink"> 

<xsl:output method="xml" indent="no"/> 

<xsl:template match="*"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="ref//*"> 
    <xsl:element name="{local-name()}"><xsl:apply-templates/></xsl:element><xsl:if test="following-sibling::*"><xsl:text> </xsl:text></xsl:if> 
</xsl:template> 

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

</xsl:stylesheet> 
0

XSLT 3.0(和撒克遜)有<xsl:output method="xml" indent="yes" suppress-indentation="ref"/>這是專門爲此目的而設計的。如果你不能利用這個優勢,我會建議使用indent =「no」,並使用xsl:value-of或xsl:text手工添加ref元素之間的空白(以及其他任何你想要的)。

相關問題