Kalamari Posted November 22, 2011 Share Posted November 22, 2011 Example 1<?xml version="1.0" encoding="Windows-1257"?><?xml-stylesheet type="text/xsl" href="XSLFILE.xsl"?><SALES><PERSON><DATA NAME="John" x="27"/><DATA NAME="Mary" x="67"/></PERSON><METADATA><DATA VALUE="x"/><DATA VALUE="NAME"/></METADATA></SALES>output 27 John 67 Mary Exaple 2:<?xml version="1.0" encoding="Windows-1257"?><?xml-stylesheet type="text/xsl" href="XSLFILE.xsl"?><SALES><PERSON><DATA NAME="John" x="27" y="33"/><DATA NAME="Mary" x="67" y="414"/></PERSON><METADATA><DATA VALUE="x"/><DATA VALUE="y"/><DATA VALUE="NAME"/></METADATA></SALES>SALES/PERSON/DATA attribute names and order is always given by SALES/METADATA/DATA attribute. My question is, how to write xsl to get SALES/PERSON/DATA attribute values using METADATA attributes? Link to comment Share on other sites More sharing options...
Martin Honnen Posted November 23, 2011 Share Posted November 23, 2011 Here is a sample stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="sep" select="' '"/> <xsl:output method="text"/> <xsl:variable name="columns" select="/SALES/METADATA/DATA/@VALUE"/> <xsl:template match="/"> <xsl:apply-templates select="SALES/PERSON/DATA"/> </xsl:template> <xsl:template match="PERSON/DATA"> <xsl:apply-templates select="$columns"> <xsl:with-param name="data" select="."/> </xsl:apply-templates> <xsl:text></xsl:text> </xsl:template> <xsl:template match="METADATA/DATA/@VALUE"> <xsl:param name="data"/> <xsl:if test="position() > 1"> <xsl:value-of select="$sep"/> </xsl:if> <xsl:value-of select="$data/@*[local-name() = current()]"/> </xsl:template> </xsl:stylesheet> Output for the input <SALES><PERSON><DATA NAME="John" x="27"/><DATA NAME="Mary" x="67"/></PERSON><METADATA><DATA VALUE="x"/><DATA VALUE="NAME"/></METADATA></SALES> is 27 John67 Mary Link to comment Share on other sites More sharing options...
Kalamari Posted November 28, 2011 Author Share Posted November 28, 2011 thank jou Martin. exactly what i need. i just try to put the output into the <table> Link to comment Share on other sites More sharing options...
Margus Posted October 1, 2015 Share Posted October 1, 2015 How to modify this xsl in case when xml output should look like this: <table><tr><td>27</td><td>33</td><td>John</td></tr><tr><td>67</td><td>414</td><td>Mary</td></tr></table> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now