[英]XSLT: how to remove all attributes from specified element except for some
I am trying to remove some attributes with a XSLT from XML data.我正在尝试从 XML 数据中删除一些带有 XSLT 的属性。
I have the following 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>
and I want to remove all attributes from all tt:style
elements except for我想从所有
tt:style
元素中删除所有属性,除了
In the example XML only tts:fontWeight should be removed but in other XMLs there might be other attributes which are not allowed per XSD.在示例 XML 中,仅应删除 tts:fontWeight,但在其他 XML 中,可能存在其他属性,XSD 不允许。
Thanks a lot in advance.提前非常感谢。
How about:怎么样:
XSLT 1.0 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.