XSLT: как удалить все атрибуты из указанного элемента, кроме некоторых - PullRequest
0 голосов
/ 05 мая 2020

Я пытаюсь удалить некоторые атрибуты с помощью XSLT из данных XML.

У меня есть следующие XML:

<tt:tt xmlns:tt="http://www.w3.org/ns/ttml"
       xmlns:tts="http://www.w3.org/ns/ttml#styling"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
       xmlns:ttp="http://www.w3.org/ns/ttml#parameter"
       xmlns:ebuttdt="urn:ebu:tt:datatypes"
       xmlns:ebuttm="urn:ebu:tt:metadata"
       xmlns:ebutts="urn:ebu:tt:style"
       xmlns:ebuttExt="urn:ebu:tt:extension"
       ttp:timeBase="media"
       ttp:cellResolution="50 30"
       xml:lang="de">
    <tt:head>
        <tt:metadata>
            <ebuttm:documentMetadata>
                <ebuttm:documentEbuttVersion>v1.0</ebuttm:documentEbuttVersion>
            </ebuttm:documentMetadata>
        </tt:metadata>
        <tt:styling>
            <tt:style xml:id="S1"
                   tts:fontSize="160%"
                   tts:fontFamily="Verdana, Arial, Tiresias"
                   tts:lineHeight="125%"/>
            <tt:style xml:id="S2"
                   tts:fontSize="200%"
                   tts:fontFamily="Arial"
                   tts:textAlign="left"
                      />
            <tt:style xml:id="S3"
                   tts:color="#ffffff"
                   tts:backgroundColor="#000000c2"
                   tts:fontWeight="normal"/>
            <tt:style xml:id="S4"
                   tts:fontSize="200%"
                   tts:fontFamily="Arial"
                   tts:textAlign="center"/>
            <tt:style xml:id="S5"
                   tts:color="#00ffff"
                   tts:backgroundColor="#000000c2"
                   tts:fontWeight="normal"/>
            <tt:style xml:id="S6"
                   tts:color="#ffff00"
                   tts:backgroundColor="#000000c2"
                   tts:fontWeight="normal"/>
            <tt:style xml:id="S7"
                   tts:color="#00ff00"
                   tts:backgroundColor="#000000c2"
                   tts:fontWeight="normal"/>
            <tt:style xml:id="S8" tts:color="#ffffff" tts:backgroundColor="#000000c2"/>
            <tt:style xml:id="S9"
                   tts:color="#ffffff"
                   tts:backgroundColor="#ff0000c2"
                   tts:fontWeight="normal"/>
        </tt:styling>
        <tt:layout>
            <tt:region xml:id="R1"
                    tts:origin="8% 7%"
                    tts:extent="84% 86%"
                    tts:displayAlign="after"/>
        </tt:layout>
    </tt:head>
    <tt:body>
        <tt:div style="S1">
            <tt:p xml:id="C1"
               region="R1"
               style="S2"
               begin="10:00:00.000"
               end="10:00:02.367">
                <tt:span style="S3">Personen und ihre Farben:</tt:span>
            </tt:p>
            <tt:p xml:id="C2"
               region="R1"
               style="S2"
               begin="10:00:02.867"
               end="10:00:04.734">
                <tt:span style="S3">Roland Heilmann, Klinikleiter</tt:span>
            </tt:p>
            <tt:p xml:id="C3"
               region="R1"
               style="S2"
               begin="10:00:05.200"
               end="10:00:06.800">
                <tt:span style="S3">Kathrin Globisch, Anästhesistin</tt:span>
            </tt:p>
            <tt:p xml:id="C4"
               region="R1"
               style="S4"
               begin="10:00:07.333"
               end="10:00:08.900">
                <tt:span style="S5">Martin Stein, Oberarzt</tt:span>
            </tt:p>
            <tt:p xml:id="C5"
               region="R1"
               style="S2"
               begin="10:00:09.400"
               end="10:00:11.200">
                <tt:span style="S3">Sarah Marquardt, Verwaltungschefin</tt:span>
            </tt:p>
            <tt:p xml:id="C6"
               region="R1"
               style="S2"
               begin="10:00:12.533"
               end="10:00:14.600">
                <tt:span style="S3">Arzu Ritter</tt:span>
                <tt:span style="S6">   Philipp Brentano</tt:span>
            </tt:p>
        </tt:div>
    </tt:body>
</tt:tt>

, и я хочу удалить все атрибуты из все элементы tt:style, кроме

  • xml: id
  • tts: fontFamily
  • tts: fontSize
  • tts: lineHeight
  • tts: textAlign
  • tts: color
  • tts: backgroundColor

В примере XML следует удалить только tts: fontWeight, но другие XML могут быть другие атрибуты, которые не разрешены в XSD.

Заранее большое спасибо.

1 Ответ

1 голос
/ 05 мая 2020

Как насчет:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tt="http://www.w3.org/ns/ttml"
xmlns:tts="http://www.w3.org/ns/ttml#styling">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="tt:style">
    <xsl:copy>
        <xsl:apply-templates select="@xml:id|@tts:fontFamily|@tts:fontSize|@tts:lineHeight|@tts:textAlign|@tts:color|@tts:backgroundColor|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...