Konwersja elementów XML na atrybuty XML przy użyciu XSLT
Mamy bieżący system, który wysyła plik XML, który jest w następującym formacie:
<INVENTORY>
<ITEM>
<SERIALNUMBER>something</SERIALNUMBER>
<LOCATION>something</LOCATION>
<BARCODE>something</BARCODE>
</ITEM>
</INVENTORY>
Muszę użyć tych danych, aby załadować je do Standardowej siatki. NET 2.0. Ale siatka potrzebuje XML w następującym formacie:
<INVENTORY>
<ITEM serialNumber="something" location="something" barcode="something">
</ITEM>
</INVENTORY>
Tzn. węzły potomne elementu muszą zostać przekonwertowane na atrybuty węzła elementu.
Czy ktoś wie jak można to zrobić za pomocą XSLT?
5 answers
To powinno zadziałać:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="INVENTORY">
<INVENTORY>
<xsl:apply-templates/>
</INVENTORY>
</xsl:template>
<xsl:template match="ITEM">
<ITEM>
<xsl:for-each select="*">
<xsl:attribute name="{name()}">
<xsl:value-of select="text()"/>
</xsl:attribute>
</xsl:for-each>
</ITEM>
</xsl:template>
</xsl:stylesheet>
HTH
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-17 18:16:46
Oto prawdopodobnie najprostsze rozwiązanie , które przekonwertuje dowolne elementy potomne ITEM
na swoje atrybuty i odtworzy Wszystko inne tak, jak jest, podczas konwersji nazw elementów na dowolne pożądane nazwy atrybutów:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- -->
<xsl:strip-space elements="*"/>
<xsl:variable name="vrtfNameMapping">
<item name="SERIALNUMBER" newName="serialNumber"/>
<item name="LOCATION" newName="location"/>
<item name="BARCODE" newName="barcode"/>
</xsl:variable>
<!-- -->
<xsl:variable name="vNameMapping" select=
"document('')/*/xsl:variable[@name='vrtfNameMapping']"/>
<!-- -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- -->
<xsl:template match="ITEM/*">
<xsl:attribute name=
"{$vNameMapping/*[@name=name(current())]/@newName}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Gdy powyższa transformacja zostanie zastosowana na dostarczonym dokumencie XML :
<INVENTORY>
<ITEM>
<SERIALNUMBER>something</SERIALNUMBER>
<LOCATION>something</LOCATION>
<BARCODE>something</BARCODE>
</ITEM>
</INVENTORY>
:
<INVENTORY>
<ITEM serialNumber="something" location="something" barcode="something"/>
</INVENTORY>
Zwróć uwagę na:
Korzystanie z reguła tożsamości
Zastosowanie
<xsl:strip-space elements="*"/>
Zastosowanie zmiennej
vrtfNameMapping
bez funkcji rozszerzeniaxxx:node-set()
.Fakt, że obsługujemy każde mapowanie między nazwą a nową nazwą, nie tylko zwykłą niższą obudową.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-18 02:21:19
Te dwa szablony powinny to zrobić:-
<xsl:template match="ITEM">
<ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}" />
</xsl:template>
<xsl:template match="INVENTORY">
<INVENTORY>
<xsl:apply-templates />
</INVENTORY>
</xsl:template>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-11 11:20:54
To powinno wystarczyć:
<xsl:for-each select="//ITEM">
<xsl:element name="ITEM">
<xsl:attribute name="serialNumber">
<xsl:value-of select="SERIALNUMBER"/>
</xsl:attribute>
<xsl:attribute name="location">
<xsl:value-of select="LOCATION"/>
</xsl:attribute>
<xsl:attribute name="barcode">
<xsl:value-of select="BARCODE"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
Lub używając skrótu Dawida:
<xsl:for-each select="//ITEM">
<ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}"/>
</xsl:for-each>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-17 18:09:05
Jeśli Twoje źródło wygląda tak:
<row><a>1</a><b>2</b></row>
I chcesz, aby wyglądało to tak:
<row a="1" b="2" />
Wtedy ten XSLT powinien działać:
<xsl:template match="row">
<row a="{a}" b="{b}" />
</xsl:template>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-17 18:10:21