Entfernen von XML-Attributen mit XSLT

TresPuntos

Cadet 4th Year
Registriert
Juni 2018
Beiträge
113
Hallo,

ich suche momentan nach einer Lösung für mein XSLT-Problem.
Das einzelne löschen von Attributen in einem Knoten klappt super, allerdings schaffe ich es nicht das Ganze universell umzusetzen.
Sprich jedes Attribut version, Version und LastChanged soll entfernt werden, egal wo es sich befindet.
->Beispiel:
XML:
<?xml version="1.0" encoding="utf-8"?>
<Settings Version="0.0.0.0">
  <Setting Key="123" Description="" LastChanged="" version="1.9">
    <Value>
      <XtraSerializer version="1.0" application="View">
        <property name="#LayoutVersion" />
        <test version="2.0" />
        </XtraSerializer>
    </Value>
    </Setting>
</Settings>

Sollte dann so aussehen:
XML:
<?xml version="1.0" encoding="utf-8"?>
<Settings>
  <Setting Key="123" Description="">
    <Value>
      <XtraSerializer application="View">
        <property name="#LayoutVersion" />
        <test/>
        </XtraSerializer>
    </Value>
    </Setting>
</Settings>
 
https://xsltfiddle.liberty-development.net/3NJ38YS

XML:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*[translate(name(.), $smallcase, $uppercase)!='VERSION' and translate(name(.), $smallcase, $uppercase)!='LASTCHANGED' ]|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Müsste so gehen:) ohne Gewähr^^
 
Zuletzt bearbeitet:
Mein bestehende Datei sieht, wie unten aus. Wie könnte ich das Ganze den einbinden und wie müsste ich die aktuellen !='Version' etc. rausnehmen? Aktuell habe ich es für spezifische Knoten festgelegt und nun wollte ich es rausnehmen, um diese 3 Attribute bei jedem Knoten zu entfernen
XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@*">
        <xsl:sort select="name()"/>
        <xsl:sort select="@text()"/>
      </xsl:apply-templates>
    
      <xsl:apply-templates select="*">
        <xsl:sort select="name()"/>
        <xsl:sort select="@*"/>
        <xsl:sort select="count(@*)"/>
        <xsl:sort select="@text()"/>
        <xsl:sort select="text()"/>
        <xsl:sort select="descendant::text()"/>
      </xsl:apply-templates>
      <xsl:if test="text() != ''">
        <xsl:value-of select="text()"/>
      </xsl:if>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Settings/Setting/Value//*[property]">
    <xsl:copy>
      <xsl:copy-of select="@*[name(.)!='version']"/>
      <xsl:apply-templates select="property">
        <xsl:sort select="translate(@name, 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        <!--Case insensitives sortieren-->
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="/Settings">
    <xsl:copy>
      <xsl:copy-of select="@*[name(.)!='Version']"/>
      <xsl:apply-templates select="Setting">
        <xsl:sort select="@Key"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="/Settings/Setting">
    <xsl:copy>
      <xsl:copy-of select="@*[name(.)!='Version' and name(.)!='LastChanged' ]"/>
      <xsl:apply-templates select="Value">
      </xsl:apply-templates>
      <xsl:apply-templates select="*[name(.)!='Value']">
        <xsl:sort select="name(.)"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
 
Hmmm...
lass die XSLs nacheinander laufen...
wäre am einfachsten
 
Zurück
Oben