Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 一个节点的所有子节点在输出中显示在同一行上_Xml_Xslt - Fatal编程技术网

Xml 一个节点的所有子节点在输出中显示在同一行上

Xml 一个节点的所有子节点在输出中显示在同一行上,xml,xslt,Xml,Xslt,我有一些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>

我有一些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>
我可以使用以下样式表展开元素:

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


但现在,每个元素将显示在不同的行上。我也尝试过使用
,但这似乎根本没有改变输出。

XSLT 3.0(和Saxon)有
,它正是为此而设计的。如果您不能利用这一点,我建议您使用indent=“no”,并使用xsl:value of或xsl:text“手动”在ref元素(以及您想要的任何地方)之间添加空格。

您可以尝试的是将“indent”选项设置为“no”,然后不再使用
条带空间,它将从输入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>


为什么这很重要?您正在输出XML。您是否尝试过将缩进设置为“否”<代码>
。您还需要执行
,否则将复制仅限空白的节点。这一点很重要,因为仅限空白的节点将被保留,并且在导入Adobe InDesign时会产生问题。计划在进口时应用此tansformation。我尝试了
indent=“no”
,但这似乎将所有内容放在一行上,这是不需要的。
<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>