Jump to content

Have a hard time understand Attributes and Values in XML


EENew02

Recommended Posts

Hi,I have this xml file that someone send it to me and I have a hard time understand the code below can someone help me interpret the code below? What code below is a sample output message. If I need to translate this code and put into my XSLT, how do I do it?<products xmlns="http://types.networx.gci.com/jaws"><addItem>Borad Band Cable Modem</addItem> <attributes><attribute xmlns="http://types.net.abc.com/jaws">Speed</attribute> <value xmlns="http://types.net.abc.com/jaws">56MBPS</value> </attributes><attributes> <attribute xmlns="http://types.net.abc.com/jaws">Voltage</attribute> <value xmlns:ns_products="http://types.net.abc.com/jaws">90 V</value> </attributes>Thanks in advance

Link to comment
Share on other sites

Well, XML is a free language. Anyone can define anything as long as it follows the common rules.What do you mean by "sample output message"? This is very broad definition. You can output the contents of any element or attribute with XSLT, so what exactly do you need?By the way, this seems like one of the very few cases where using attributes (real I mean) seems more appropriate. Also declaring the namespaces with a prefix seems more appropriate too. No matter...So what exactly do you need to get from this XML? If it's nothing specific, just play with the XPath expression(s).

Link to comment
Share on other sites

Well, XML is a free language. Anyone can define anything as long as it follows the common rules.What do you mean by "sample output message"? This is very broad definition. You can output the contents of any element or attribute with XSLT, so what exactly do you need?By the way, this seems like one of the very few cases where using attributes (real I mean) seems more appropriate. Also declaring the namespaces with a prefix seems more appropriate too. No matter...So what exactly do you need to get from this XML? If it's nothing specific, just play with the XPath expression(s).
Hmm. The code I got from the above message is coming from XML document fragment from the SOAP envelope. This is what it looks like: <ns_products:attributes> <ns_products:attribute xmlns:ns_products="http://types.abc.com">Speed</ns_products:attribute> <ns_products:value xmlns:ns_products="http://types.abc.com">56 MBPS</ns_products:value> </ns_products:attributes>- <ns_products:attributes> <ns_products:attribute xmlns:ns_products="http://types.abc.com">Voltage</ns_products:attribute> <ns_products:value xmlns:ns_products="http://types.abc.com">900 V</ns_products:value> </ns_products:attributes>By looking at the message above I want to be able to tranform the above code in my xslt. This is what I did but I am not sure it's right. In my xslt I have as this:- <attributes>- <xsl:for-each select="ServiceAddChange">- <attribute> <xsl:value-of select="@name" /> </attribute>- <value> <xsl:value-of select="@value" /> </value> </xsl:for-each> </attributes>
Link to comment
Share on other sites

The "@" selects XML attributes, not elements called "attribute" with a certain name. Try this sort of XSLT:

<xsl:template match="ns_products:attributes">  <attributes>	<xsl:for-each select="ns_products:attribute">	  <attribute>		<xsl:value-of select="." /> 	  </attribute>	  <value>		<xsl:value-of select="following-sibling::*[position()=1]" /> 	  </value>	</xsl:for-each>  </attributes></xsl:template>

This of course will only work if you want what I think you want. After all, that's not the complete SOAP envelope you're showing and I'm not sure if the output:

<attributes><attribute>Speed</attribute> <value>56 MBPS</value> </attributes> <attributes><attribute>Voltage</attribute> <value>900 V</value> </attributes>

Is exactly what you need, though even if that's not right, it's still more readable then the above :) .

Link to comment
Share on other sites

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...