xml - XSLT: flatten tree -
i'd convert kodi-compatible xml xtreamer-compatible xml. input looks this:
<?xml version="1.0" encoding="utf-8"?> <movie xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <id>something</id> <title>something</title> <originaltitle>something</originaltitle> ...and on... several unknown nodes <actor> <name>name1</name> <role>role1</role> <order>0</order> </actor> <actor> <name>name2</name> <role>role2</role> <order>1</order> </actor> <actor> <name>name3</name> <role>role3</role> <order>2</order> </actor> ...several other unknown nodes... </movie>
what i'd have is:
<?xml version="1.0" encoding="utf-8"?> <movie xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <id>something</id> <title>something</title> <originaltitle>something</originaltitle> ...and on... several unknown nodes <actor> <name>name1</name> <name>name2</name> <name>name3</name> </actor> ...several other unknown nodes... </movie>
...so drop every child item of actor besides name , put names 1 common actor tag. rest of document should stay unchanged.
what tried is:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns="http://www.w3.org/tr/rec-html40"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="/movie"> <actor> <xsl:for-each select="actor"> <xsl:apply-templates select="name"/> </xsl:for-each> </actor> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
what is:
<?xml version="1.0"?> <actor xmlns="http://www.w3.org/tr/rec-html40"><name xmlns="">name1</name><name xmlns="">name2</name><name xmlns="">name3</name></actor> <id>something</id> <title>something</title> <originaltitle>something</originaltitle> ...and on... several unknown nodes <actor> <name>name1</name> <role>role1</role> <order>0</order> </actor> <actor> <name>name2</name> <role>role2</role> <order>1</order> </actor> <actor> <name>name3</name> <role>role3</role> <order>2</order> </actor> ...several other unknown nodes...
some things don't work work wanted:
- all actor-nodes , children still in output.
- the actor-name-name-name-actor-block:
- replaces movie-tag
- is in beginning, not @ position, actor-nodes used before
- has no line breaks
any appreciated.
i have come this:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="actor[1]"> <xsl:copy> <xsl:copy-of select="../actor/name" /> </xsl:copy> </xsl:template> <xsl:template match="actor" /> </xsl:stylesheet>
instead of using movie element, transform actors. prevents movie tag disappearing, , keep actors in place.
there 2 rules actor, first element select names. other remain blank.
the line breaks still not correct though. think it's bit weird focused on line breaks, not matter in xml. perhaps want more readable.
Comments
Post a Comment