Announcement

Collapse
No announcement yet.

node kopieren ohne alle attribute ??

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • node kopieren ohne alle attribute ??

    Hallo,
    ich weiß nicht, ob das eine dumme anfängerfrage oder eine ernste schwierigkeit ist:

    Ich will einen knoten mit inhalt kopieren, dabei aber nicht alle attribute übernehmen (ich würde die nicht gewünschten am liebsten über eine blacklist entfernen, es geht aber auch über eine whitelist):

    Quelle:
    <mCL:ACTIONS>
    <VALUE ITEM_NAME="GFDestination1" GUIDlgRef="MailAddress1">[email protected]</VALUE>
    </mCL:ACTIONS>

    Soll werden zu:
    <ACTIONS>
    <VALUE ITEM_NAME="GFDestination1">[email protected]</VALUE>
    <ACTIONS>

    Wenn ich nun <xsl:copy-of select="."/> verwende, wird nix weggelassen.
    Wenn ich
    <xsl:copy use-attribute-sets="actionsAttributes">
    <xsl:apply-templates"/>
    </xsl:copy>
    verwende (wobei "actionsAttributes" ein xsl:attribute-set ist), wird das attribut leer eingefügt.

    Außerdem wird mir immer ein quell-namespace eingefügt, den ich im ziel-xml nicht brauchen kann.

    Was kann ich tun?

    Danke für alle antworten!

  • #2
    Mit XSLT 2.0
    Code:
    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0"
      xmlns:mCL="http://example.com/"
      exclude-result-prefixes="mCL">
      
      <xsl:template match="@* | node()">
        <xsl:copy copy-namespaces="no">
          <xsl:apply-templates select="@*, node()"/>
        </xsl:copy>
      </xsl:template>
      
      <xsl:template match="mCL:ACTIONS">
        <ACTIONS>
          <xsl:apply-templates select="@*, node()"/>
        </ACTIONS>
      </xsl:template>
      
      <xsl:template match="VALUE/@GUIDlgRef"/>
    
    </xsl:stylesheet>
    wird aus
    Code:
    <mCL:ACTIONS xmlns:mCL="http://example.com/">
    <VALUE ITEM_NAME="GFDestination1" GUIDlgRef="MailAddress1">[email protected]</VALUE>
    </mCL:ACTIONS>
    das Resultat
    Code:
    <ACTIONS>
    <VALUE ITEM_NAME="GFDestination1">[email protected]</VALUE>
    </ACTIONS>

    Comment


    • #3
      ich bevorzug lieber xsl1

      Der beste Weg ist Template in seinen Ursprünglichen Form zu nutzen
      und nicht mit kurz Schreibweisen zu tricksen oder xslt2 zu nutzen
      da ja alle Browser nur xslt1 können.


      ein Template beschreibt was passiert wenn ein Knoten gefunden wird
      der mit Namen des Template über ein stimmmt.
      zum Test xml erweiter
      Code:
      <?xml version="1.0"?>
      <root>
      	<mCL:ACTIONS xmlns:mCL="http://example.com/">
      		<VALUE ITEM_NAME="GFDestination1" GUIDlgRef="MailAddress1">[email protected]</VALUE>
      	</mCL:ACTIONS>
      
      	<mCL:ACTIONS xmlns:mCL="http://example.com/">
      		<VALUE ITEM_NAME="GFDestination2" GUIDlgRef="MailAddress2">[email protected]</VALUE>
      	</mCL:ACTIONS>
      </root>
      Code:
      <?xml version="1.0"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mCL="http://example.com/" exclude-result-prefixes="mCL">
      	<xsl:output indent="yes" method="xml"/>
      	<xsl:template match="/">
      		<root>
      			<xsl:apply-templates select="root"/>
      		</root>
      	</xsl:template>
      	<xsl:template match="root">
      		<xsl:apply-templates/>
      	</xsl:template>
      
      
      	<xsl:template match="mCL:ACTIONS">
      		<ACTIONS>
      			<xsl:apply-templates select="VALUE"/>
      		</ACTIONS>
      	</xsl:template>
      
      	<xsl:template match="VALUE">
      		<VALUE ITEM_NAME="{@ITEM_NAME}"><xsl:value-of select="."/>
      		</VALUE>
      	</xsl:template>
      </xsl:stylesheet>
      Ergebnis
      Code:
      <?xml version='1.0' encoding='UTF-8' ?>
      <root>
      	
        <ACTIONS>
          <VALUE ITEM_NAME="GFDestination1">[email protected]</VALUE>
        </ACTIONS>
      
      	
        <ACTIONS>
          <VALUE ITEM_NAME="GFDestination2">[email protected]</VALUE>
        </ACTIONS>
      
      </root>

      wenn man nicht mit Browser arbeitet geht der Weg über xslt 2
      machmal wesentlich leichter da mehr Funktion vorhanden sind

      Comment


      • #4
        Vielen dank!

        Die xslt2.0 variante konnte ich nicht verwenden, da ich für ein Office2007-AddIn entwickle und dort funktioniert copy-namespaces nicht.

        Die andere variante funktioniert wie gewünscht, nur das das attribut ITEM_NAME (oder ein weiteres), das optional sein soll, immer angelegt wird und dann eben leer ist, wenn in der quelle nicht vorhanden. Gibt es hierfür auch eine einfache lösung?

        Comment


        • #5
          Code:
          <?xml version="1.0"?>
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mCL="http://example.com/" exclude-result-prefixes="mCL">
          	<xsl:output indent="yes" method="xml"/>
          	<xsl:template match="/">
          		<root>
          			<xsl:apply-templates select="root"/>
          		</root>
          	</xsl:template>
          	<xsl:template match="root">
          		<xsl:apply-templates/>
          	</xsl:template>
          	<xsl:template match="mCL:ACTIONS">
          		<ACTIONS>
          			<xsl:apply-templates select="VALUE"/>
          		</ACTIONS>
          	</xsl:template>
          	<xsl:template match="VALUE">
          		<VALUE>
          		<xsl:if test="@ITEM_NAME">
          		<xsl:attribute name="ITEM_NAME"><xsl:value-of select="@ITEM_NAME"/></xsl:attribute>
          		</xsl:if>
          		<xsl:value-of select="."/>
          		</VALUE>
          	</xsl:template>
          </xsl:stylesheet>

          Comment

          Working...
          X