Announcement

Collapse
No announcement yet.

Leeres xmlns Attribut bei XML-Transformation

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

  • Leeres xmlns Attribut bei XML-Transformation

    Ich möchte gerne eine XML mittels XSLT transformieren. Dabei wird bei einem Element immer ein leere xmlns-Attribute übergeben. Meine bisherige Google-Recherche liefert in meinen Augen für mein Problem nicht die passende Lösung. Vielleicht liegt es auch an meinem Verständnis und ich suche nun Hilfe hier.

    Hier das Quell-XML was ich transformieren möchte.
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <Root>
      <Row>
        <KENUM>MONITOR1</KENUM>
        <ALN_KCIINVENTARNR>12345</ALN_KCIINVENTARNR>
        <STATUS>BETRIEB</STATUS>
        <LOCATION>LOC</LOCATION>
        <ALN_KCINUTZUNGSART>Mobil</ALN_KCINUTZUNGSART>
      </Row>
    </Root>
    Hier das für die Transformation verwendete XSL.

    Code:
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
      <xsl:strip-space elements="*"/>
      <xsl:template match="@*|node()">
        <xsl:if test=". != '' or ./@* != ''">
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:if>
      </xsl:template>
      <xsl:template match="Root">
        <SyncSet xmlns="http://myns.de" baseLanguage="DE" transLanguage="DE" event="0">
          <MXOSSet>
            <xsl:apply-templates select="@*|node()"/>
          </MXOSSet>
        </SyncSet>
      </xsl:template>
      <xsl:template match="Row">
        <KE>
          <xsl:apply-templates select="node()"/>
        </KE>
      </xsl:template>
      <xsl:template match="*[(contains(name(),'ALN_')) and current()!='']">
        <SPEC>
          <ASSETATTRID>
            <xsl:value-of select="substring-after(name(),'ALN_')" />
          </ASSETATTRID>
          <ALNVALUE>
            <xsl:value-of select="." />
          </ALNVALUE>
          <SECTION />
          <TABLEVALUE />
        </SPEC>
      </xsl:template>  
    </xsl:stylesheet>
    Hier der Output des Ziel-XML mit dem leeren xmlns-Attribut. Dieses Attribut bekomme ich nicht raus. Es muss aber da weg, da das System wo ich dieses XML importieren möchte, damit nicht zurecht kommt.

    Code:
    <SyncSet xmlns="http://myns.de" baseLanguage="DE" transLanguage="DE" event="0">
       <MXOSSet>
          <KE xmlns="">
             <KENUM>MONITOR1</KENUM>
             <SPEC>
                <ASSETATTRID>KCIINVENTARNR</ASSETATTRID>
                <ALNVALUE>12345</ALNVALUE>
                <SECTION/>
                <TABLEVALUE/>
             </SPEC>
             <STATUS>BETRIEB</STATUS>
             <LOCATION>LOC</LOCATION>
             <SPEC>
                <ASSETATTRID>KCINUTZUNGSART</ASSETATTRID>
                <ALNVALUE>Mobil</ALNVALUE>
                <SECTION/>
                <TABLEVALUE/>
             </SPEC>
          </KE>
       </MXOSSet>
    </SyncSet>

  • #2
    Du musst xmlns="http://myns.de" in das Wurzelelement des Stylesheets packen:
    Code:
    xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://myns.de">
    dann gilt es für alle Resultatselemente, die erzeugt werden.

    Comment


    • #3
      Originally posted by Martin Honnen View Post
      Du musst xmlns="http://myns.de" in das Wurzelelement des Stylesheets packen:
      Code:
      xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://myns.de">
      dann gilt es für alle Resultatselemente, die erzeugt werden.
      In diesem Fall bekomme ich bei gleichbleibenden Quell-XML und folgendem XLS (Namespace zusätzlich in xsl-Wurzel)...

      Code:
      <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://myns.de">
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
        <xsl:strip-space elements="*"/>
        <xsl:template match="@*|node()">
          <xsl:if test=". != '' or ./@* != ''">
            <xsl:copy>
              <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
          </xsl:if>
        </xsl:template>
        <xsl:template match="Root">
          <SyncSet xmlns="http://myns.de" baseLanguage="DE" transLanguage="DE" event="0">
            <MXOSSet>
              <xsl:apply-templates select="@*|node()"/>
            </MXOSSet>
          </SyncSet>
        </xsl:template>
        <xsl:template match="Row">
          <KE>
            <xsl:apply-templates select="node()"/>
          </KE>
        </xsl:template>
        <xsl:template match="*[(contains(name(),'ALN_')) and current()!='']">
          <SPEC>
            <ASSETATTRID>
              <xsl:value-of select="substring-after(name(),'ALN_')" />
            </ASSETATTRID>
            <ALNVALUE>
              <xsl:value-of select="." />
            </ALNVALUE>
            <SECTION />
            <TABLEVALUE />
          </SPEC>
        </xsl:template>  
      </xsl:stylesheet>
      ... diesen Output.

      Code:
      <SyncSet xmlns="http://myns.de" baseLanguage="DE" transLanguage="DE" event="0">
         <MXOSSet>
            <KE>
               <KENUM xmlns="">MONITOR1</KENUM>
               <SPEC>
                  <ASSETATTRID>KCIINVENTARNR</ASSETATTRID>
                  <ALNVALUE>12345</ALNVALUE>
                  <SECTION/>
                  <TABLEVALUE/>
               </SPEC>
               <STATUS xmlns="">BETRIEB</STATUS>
               <LOCATION xmlns="">LOC</LOCATION>
               <SPEC>
                  <ASSETATTRID>KCINUTZUNGSART</ASSETATTRID>
                  <ALNVALUE>Mobil</ALNVALUE>
                  <SECTION/>
                  <TABLEVALUE/>
               </SPEC>
            </KE>
         </MXOSSet>
      </SyncSet>
      Jetzt haben sich die xmlns-Attribute auch noch vermehrt. ;-) Woran kann das liegen?

      Comment


      • #4
        Der Code kopiert manche Elemente aus dem Eingabedokument, diese sind aber in keinem Namensraum, deshalb wird dann im Resultat xmlns="" angezeigt.
        Du musst diese Elemente also nicht einfach kopieren, sondern per Template transformieren:

        Code:
        <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://myns.de">
          <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
          <xsl:strip-space elements="*"/>
          <xsl:template match="@*|node()">
            <xsl:if test=". != '' or ./@* != ''">
              <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
            </xsl:if>
          </xsl:template>
          <xsl:template match="Root">
            <SyncSet baseLanguage="DE" transLanguage="DE" event="0">
              <MXOSSet>
                <xsl:apply-templates select="@*|node()"/>
              </MXOSSet>
            </SyncSet>
          </xsl:template>
          <xsl:template match="Row">
            <KE>
              <xsl:apply-templates select="node()"/>
            </KE>
          </xsl:template>
          <xsl:template match="*[(contains(name(),'ALN_')) and current()!='']">
            <SPEC>
              <ASSETATTRID>
                <xsl:value-of select="substring-after(name(),'ALN_')" />
              </ASSETATTRID>
              <ALNVALUE>
                <xsl:value-of select="." />
              </ALNVALUE>
              <SECTION />
              <TABLEVALUE />
            </SPEC>
          </xsl:template>  
        
        <xsl:template match="Row/*[not(contains(name(),'ALN_'))]">
          <xsl:element name="{local-name()}">
             <xsl:apply-templates/>
          </xsl:element>
        </xsl:template>
        
        </xsl:stylesheet>

        Comment


        • #5
          Originally posted by Martin Honnen View Post
          Der Code kopiert manche Elemente aus dem Eingabedokument, diese sind aber in keinem Namensraum, deshalb wird dann im Resultat xmlns="" angezeigt.
          Du musst diese Elemente also nicht einfach kopieren, sondern per Template transformieren:
          Vielen Dank! Jetzt arbeitet alles wie gewollt.

          Das macht mir auch deutlich, dass mir die Arbeitsweise des Parsers bzw. der Transformation noch nicht ganz klar ist. Da hab ich noch Nachholebedarf.

          Comment

          Working...
          X