XML | XML-Schema | XPath | XSL-T | XSL-FO | XQuery | XProc | SVG |
XSL-T / XSLT-Tipps / Konvertierung von XML nach XML / Konvertierung von Elementen in Attribute
![]() |
![]() |
➪ In diesem Abschnitt lesen Sie, wie die Konvertierung von Elementen in Attribute mit Pfaden und Achsen einfach umgesetzt werden kann.
Auf dieser Seite:Das Beispiel verdeutlicht die Konvertierung von Elementen in Attribute mit XPath-Achsen, speziell child::*. Hierbei geht es darum, sämtliche Childnodes anzusprechen, die nur ein einziges Mal auftreten und daher für die Konvertierung in Attribute geeignet sind.
<xsl:template match="/">
<Ergebnis>
<xsl:for-each select="/Orte/Ort">
<Stadt>
<xsl:for-each select="child::*[ count(child::*) = 0 ]">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="./text()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="Person">
<Einwohner>
<xsl:for-each select="child::*[position() < 6]">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="./text()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="Kauf">
<Einkauf>
<xsl:for-each select="child::*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="./text()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:attribute name="xpath">
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:value-of select="local-name()"/>
</xsl:for-each>
</xsl:attribute>
</Einkauf>
</xsl:for-each>
</Einwohner>
</xsl:for-each>
</Stadt>
</xsl:for-each>
</Ergebnis>
</xsl:template>
Die XPath-Funktion local-name() gibt den Namen eines Elements ohne Namespace-Präfix zurück. node-name() kann ein Präfix beinhalten, wenn ein Namespace zugeordnet ist.
Die Verwendung der local-name()-Funktion kann zu Problemen führen, wenn das XML-Dokument mit Namespaces arbeitet.
über =>, for-each und function:
Schritt 1:
//Ort => for-each( function($p_ort){<Stadt>{
for-each($p_ort/child::*[name()!='Mensch'],
function($p_ort_child){
attribute {name($p_ort_child)}{$p_ort_child/text()}
})
}</Stadt>} )
Erweitert, Schritt 2:
//Ort => for-each( function($p_ort){<Stadt>{
for-each($p_ort/child::*[name()!='Mensch'],
function($p_ort_child){
attribute {name($p_ort_child)}{$p_ort_child/text()}
} ),
for-each($p_ort/child::Mensch, function($p_mensch){
<Einwohner>{
for-each($p_mensch/child::*[name()!='Kauf'],
function($p_mensch_child){
attribute {name($p_mensch_child)}{$p_mensch_child/text()}
})}</Einwohner>
})
}</Stadt>} )
Erweitert, Schritt 3:
//Ort => for-each( function($p_ort){<Stadt>{
for-each($p_ort/child::*[name()!='Mensch'],
function($p_ort_child){
attribute {name($p_ort_child)}{$p_ort_child/text()}
} ),
for-each($p_ort/child::Mensch, function($p_mensch){
<Einwohner>{
for-each($p_mensch/child::*[name()!='Kauf'],
function($p_mensch_child){
attribute {name($p_mensch_child)}{$p_mensch_child/text()}
} ),
for-each($p_mensch/child::Kauf, function($p_kauf){<Kauf>{
for-each($p_kauf/child::*, function($p_kauf_child){
attribute {name($p_kauf_child)}{$p_kauf_child/text()}
})
}</Kauf>})
}</Einwohner>
})
}</Stadt>} )
Alternative:
//Ort => for-each( function($p_ort){ element{'Stadt'}{
for-each($p_ort/child::*[name()!='Mensch'],
function($p_ort_child){
attribute {name($p_ort_child)}{$p_ort_child/text()}
} ),
for-each($p_ort/child::Mensch, function($p_mensch){
element{'Einwohner'}{
for-each($p_mensch/child::*[name()!='Kauf'],
function($p_mensch_child){
attribute {name($p_mensch_child)}{$p_mensch_child/text()}
} ),
for-each($p_mensch/child::Kauf,
function($p_kauf){element{'Kauf'}{
for-each($p_kauf/child::*, function($p_kauf_child){
attribute {name($p_kauf_child)}{$p_kauf_child/text()}
})
}})
}
})
}})
wg / 22. März 2020
Fragen? Anmerkungen? Tipps?
Bitte nehmen Sie Kontakt zu mir auf.
V.i.S.d.P.: Wilfried Grupe * Klus 6 * 37643 Negenborn
☎ 0151. 750 360 61 * eMail: info10@wilfried-grupe.de