#xml #xslt
#xml #xslt
Вопрос:
Я пытаюсь удалить заголовок тега с помощью XSLT. Но это не удаление. Можно ли рекомендовать решение для удаления заголовка привязки.
XML-файл
<?xml version="1.0" encoding="utf-8"?>
<RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
<Content>
<a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com" title="Google Title">
Hyperlink
</a>
</Content>
</RichText>
Файл XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></xsl:output>
<xsl:template match="/ | node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*">
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*[ (self::br or self::p or self::div) and normalize-space(translate(., amp;apos; amp;apos;, amp;apos;amp;apos;)) = amp;apos;amp;apos; and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) and not(following::node()[not( (self::text() or self::br or self::p or self::div) and normalize-space(translate(., amp;apos; amp;apos;, amp;apos;amp;apos;)) = amp;apos;amp;apos; and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) )]) ]">
<!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks -->
</xsl:template>
<xsl:template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]">
<!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space -->
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Вывод:
<?xml version="1.0" encoding="utf-8"?>
<RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
<Content>
<a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com">
Hyperlink
</a>
</Content>
</RichText>
Ответ №1:
Если вы используете преобразование идентификатора, вам нужно правило для этого атрибута с пустым шаблоном.
Этот ввод
<RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
<Content>
<a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com" title="Google Title">
Hyperlink
</a>
</Content>
</RichText>
С помощью этой таблицы стилей
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml" >
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xhtml:a/@title"/>
</xsl:stylesheet>
Вывод
<RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
<Content>
<a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com">
Hyperlink
</a>
</Content>
</RichText>
Примечание: посмотрите на использование пространств имен.
Ответ №2:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a/@title"/>
</xsl:stylesheet>
You may also do using "xpath-default-namespace".