Удалить разрывы строк из XML с помощью xslt - PullRequest
0 голосов
/ 29 марта 2019

Проблема 1:

У меня есть:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Я хочу (в одну строку):

<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note> 

Задача 2

Хотите удалить внешнюю <TEST> для каждого <note>

<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>

Желаемый результат должен быть (2 строки для 2 записей:

<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

Пожалуйста, помогите мне достичь этого. С радостью предоставьте информацию, если это необходимо.

Заранее спасибо!

Ответы [ 4 ]

0 голосов
/ 30 марта 2019
    <?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"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" indent="no" encoding="UTF-8" omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//text()">
        <xsl:apply-templates select="normalize-space(.)"/>
    </xsl:template>
    <xsl:template match="TEST">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="note">
            <xsl:text>&#xA;</xsl:text>
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
0 голосов
/ 29 марта 2019
By adding your root element you can use 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
        <xsl:apply-templates select="a/TEST/note"/>
    </xsl:template>
 <xsl:template match="Test/note">
       <xsl:copy-of select="."/>
       </xsl:template>
0 голосов
/ 30 марта 2019

Обратите внимание, что ваш запрос может указывать на некоторую проблему разработки, потому что различия в сериализации между XML-документами никогда не учитываются.Кроме того, процессор XSLT может не отвечать за сериализацию.

Этот правильно сформированный вход:

<root>
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
<TEST>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</TEST>
</root>

С этой таблицей стилей:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="TEST|/*">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="note">
        <xsl:call-template name="identity"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Вывод:

<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
0 голосов
/ 29 марта 2019
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
</xsl:stylesheet>
You may use this code for single line
...