#xml #xslt #xslt-2.0
Вопрос:
Я хочу настроить отдельные узлы без добавления отступа=»нет».
<xsl:output method="html" indent="no" html-version="5"/>
При тестировании как «нормализовать пространство», так и «перевести» они работают нормально, при условии, что отступ установлен на нет, но этот параметр влияет на весь документ.
Вопрос: Как я могу удалить отступ в отдельных строках?
Пример кода вы найдете здесь: https://xsltfiddle.liberty-development.net/6qaHaQL/1
Ниже приведен тот же код:
XML:
<?xml version="1.0" encoding="utf-8" ?>
<data
xmlns:base="http://www.example.org/1"
>
<base:Content book="Alice">
<chapterOne>
Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading.
</chapterOne>
</base:Content>
<base:Content book="Alice">
<chapterTwo>
There seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes
</chapterTwo>
</base:Content>
</data>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:base="http://www.example.org/1"
>
<!--xsl:output method="html" indent="no" html-version="5"/>-->
<xsl:output method="html" html-version="5"/>
<xsl:template match="/data">
<html>
<body>
Chapter one:
<xsl:value-of select="normalize-space(base:Content/chapterOne)"/>
<!-- Test [1] Use [normalize-space]. Works if adding [indent="no"] -->
<!--xsl:value-of select="normalize-space(base:Content/chapterOne)"/>-->
<!-- Test [2] Use [translate]. Works if adding [indent="no"] -->
<!--xsl:value-of select="translate(base:Content/chapterOne, 'amp;#xA;', '')"/>-->
Chapter two:
<xsl:value-of select="base:Content/chapterTwo"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Current result:
<!DOCTYPE HTML>
<html xmlns:base="http://www.example.org/1">
<body>
Chapter one:
Alice was beginning to get very tired of sitting by her sister on the bank, and of
having nothing to do: once or twice she had peeped into the book her sister was reading.
Chapter two:
There seemed to be no use in waiting by the little door, so she went back to the table,
half hoping she might find another key on it, or at any rate a book of rules for shutting
people up like telescopes
</body>
</html>
Желаемый результат
<!DOCTYPE HTML>
<html xmlns:base="http://www.example.org/1"><body>
Chapter one:
Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading.
Chapter two:
There seemed to be no use in waiting by the little door, so she went back to the table,
half hoping she might find another key on it, or at any rate a book of rules for shutting
people up like telescopes
</body></html>
Комментарии:
1. Я не уверен, что понял, чего вы хотите достичь. Если вы выведете обычный текст внутри
body
элемента вместе сvalue-of
, пробел, который у вас есть в таблице стилей, будет скопирован. Что касается применения только таких вещей, как зачистка или отступы к определенным элементам, вы можете использоватьxsl:strip-space elements="chapterOne"
, и XSLT 3 также имеетxsl:output suppress-indentation="foo"
. Но внутри одного и того же элемента, если только вы не используетеxsl:text
для точного управления тем, какой текст и пробелы выводит XSLT, трудно добиться разных результатов с помощью одной настройки.2. Я создаю 6300 строк кода XHTML, и единственная нерешенная проблема, с которой я сталкиваюсь, — это сокращение одного предложения, которое разбивается на 2 строки. Чтобы получить утвержденные документы о результатах, мне нужно пройти тестовый стенд, в котором есть жалобы на разделение предложений. Когда я исправляю предложение вручную, я прохожу через испытательный стенд. Я удаляю все пробелы в таблице стилей и гарантирую, что исходный файл XML не содержит пробелов на этом конкретном узле.
3. Трудно сказать, откуда берется проблема. Если я правильно помню, чем в некоторых изданиях и версиях Saxon вы получаете перенос строк, я думаю, что вы можете настроить это только в PE и EE. Но я не могу сказать по вашему вопросу или вашему комментарию, в этом ли проблема.