簡體   English   中英

XSLT:如何從指定元素中刪除所有屬性,除了一些

[英]XSLT: how to remove all attributes from specified element except for some

我正在嘗試從 XML 數據中刪除一些帶有 XSLT 的屬性。

我有以下 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:字體大小
  • tts:lineHeight
  • tts:文本對齊
  • tts:顏色
  • tts:背景顏色

在示例 XML 中,僅應刪除 tts:fontWeight,但在其他 XML 中,可能存在其他屬性,XSD 不允許。

提前非常感謝。

怎么樣:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM