Jump to content
Kalamari

Xsl Get Attribute Values Using Another Element Attributes

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...