XSLT convert flat structure to array
Here's source XML:
<customers>
<firstname1>Sean</firstname1>
<lastname1>Killer</lastname1>
<sex1>M</sex1>
<firstname2>Frank</firstname2>
<lastname2>Woods</lastname2>
<sex2>M</sex2>
<firstname3>Jennifer</firstname3>
<lastname3>Lee</lastname3>
<sex3>F</sex3&g开发者_如何学编程t;
</customers>
How can I convert it to this?
<MyCustomers>
<Customer>
<Name> Sean Killer</Name>
<Sex>M</Sex>
</Customer>
<Customer>
<Name> Frank Woods</Name>
<Sex>M</Sex>
</Customer>
<Customer>
<Name>Jennifer Lee</Name>
<Sex>F</Sex>
</Customer>
</MyCustomers>
As per comments:
what if elements were not in consequent orders?
In this case (assuming XSLT 1.0) you can use translate()
to get the id of the elements and then search for the corresponding elements by correct name built using concat()
. I would change the following-sibling::
axis to the ../
(short for parent::
) to make sure to eventually catch also elements preceding the current firstname
.
<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:template match="customers">
<MyCustomers>
<xsl:apply-templates select="*[starts-with(name(),'firstname')]"/>
</MyCustomers>
</xsl:template>
<xsl:template match="*[starts-with(name(),'firstname')]">
<xsl:variable name="id" select="translate(name(),'firstname','')"/>
<Customer>
<Name><xsl:value-of select="concat(.,' ',
../*[name()=concat('lastname',$id)])"/></Name>
<Sex><xsl:value-of select="../*[name()=concat('sex',$id)]"/></Sex>
</Customer>
</xsl:template>
</xsl:stylesheet>
Obsolete answer
Assuming the fixed input document structure as shown in the question, a fine working XSLT 1.0 transform is:
<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:template match="customers">
<MyCustomers>
<xsl:apply-templates select="*[starts-with(name(),'firstname')]"/>
</MyCustomers>
</xsl:template>
<xsl:template match="*[starts-with(name(),'firstname')]">
<Customer>
<Name><xsl:value-of select="concat(.,' ',
following-sibling::*[1]
[starts-with(name(),'lastname')])"/></Name>
<Sex><xsl:value-of select="following-sibling::*[2]
[starts-with(name(),'sex')]"/></Sex>
</Customer>
</xsl:template>
</xsl:stylesheet>
Little explanation
You need XPath 1.0 function starts-with()
because of the sad name of the tags in your XML input. You can use the following-sibling::
axis to get the required following sibling tags of any element whose name starts with firstname
.
Here's an XSLT 2.0 stylesheet that will get the output you're looking for, even if they aren't in order. It also sorts by the "firstname" element names.
Sample XML input (mixed up to show different ordering):
<customers>
<lastname1>Killer</lastname1>
<sex3>F</sex3>
<firstname2>Frank</firstname2>
<firstname1>Sean</firstname1>
<lastname2>Woods</lastname2>
<sex2>M</sex2>
<firstname3>Jennifer</firstname3>
<sex1>M</sex1>
<lastname3>Lee</lastname3>
</customers>
XSLT 2.0 stylesheet (tested with Saxon-HE 9.3):
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:choose>
<xsl:when test="name()[starts-with(.,'firstname')]">
<xsl:variable name="suffix" select="substring(name(),10)"></xsl:variable>
<xsl:message><xsl:value-of select="$suffix"/></xsl:message>
<customer>
<Name>
<xsl:value-of select="concat(.,' ',/customers/*[starts-with(name(),'lastname')][ends-with(name(),$suffix)])"/>
</Name>
<Sex>
<xsl:value-of select="/customers/*[starts-with(name(),'sex')][ends-with(name(),$suffix)]"/>
</Sex>
</customer>
</xsl:when>
<xsl:when test="name()='customers'">
<MyCustomers>
<xsl:apply-templates>
<xsl:sort select="name()[starts-with(.,'firstname')]"></xsl:sort>
</xsl:apply-templates>
</MyCustomers>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output:
<MyCustomers>
<customer>
<Name>Sean Killer</Name>
<Sex>M</Sex>
</customer>
<customer>
<Name>Frank Woods</Name>
<Sex>M</Sex>
</customer>
<customer>
<Name>Jennifer Lee</Name>
<Sex>F</Sex>
</customer>
</MyCustomers>
This transformation produces the wanted result even when the children of the top element are shuffled in any arbitrary way:
<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="vNumCustomers"
select="count(/*/*) div 3"/>
<xsl:template match="/*">
<MyCustomers>
<xsl:for-each select=
"*[not(position() > $vNumCustomers)]">
<xsl:variable name="vNum" select="position()"/>
<Customer>
<Name>
<xsl:value-of select=
"concat(/*/*[name()=concat('firstname',$vNum)],
' ',
/*/*[name()=concat('lastname',$vNum)]
)
"/>
</Name>
<Sex>
<xsl:value-of select=
"/*/*[name()=concat('sex',$vNum)]
"/>
</Sex>
</Customer>
</xsl:for-each>
</MyCustomers>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document ( an arbitrary re-shuffling of the provided one):
<customers>
<sex1>M</sex1>
<lastname2>Woods</lastname2>
<lastname1>Killer</lastname1>
<sex2>M</sex2>
<firstname3>Jennifer</firstname3>
<firstname2>Frank</firstname2>
<lastname3>Lee</lastname3>
<firstname1>Sean</firstname1>
<sex3>F</sex3>
</customers>
the wanted, correct result is produced:
<MyCustomers>
<Customer>
<Name>Sean Killer</Name>
<Sex>M</Sex>
</Customer>
<Customer>
<Name>Frank Woods</Name>
<Sex>M</Sex>
</Customer>
<Customer>
<Name>Jennifer Lee</Name>
<Sex>F</Sex>
</Customer>
</MyCustomers>
Explanation:
We compute the number of customers whose data is presented. The variable
$vNumCustomers
holds this data.For each customer{i} (i = 1 to
$vNumCustomers
) we create the corresponding<Customer{i}>
element. In order to avoid using recursion, we use the Piez method here.
精彩评论