Jump to content

Xsl Get Attribute Values Using Another Element Attributes


Kalamari

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?

Link to comment
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

Link to comment
Share on other sites

  • 3 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...