Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML-XSLT doubts 1

Status
Not open for further replies.

NirvanaX

Programmer
Apr 27, 2005
2
US
I am trying to convert an XML Configuration files to text files.I had few concerns. My schema allows creation of different formats for elements. For example if i have 2 possibilities to populate an xml file:
<Dir Path="C:\MyFolder\">
<FileName> Test.txt</Name>
</Dir>
or
<Dir Path="C:\MyFolder\" >
<WildCard> *.txt</WildCard>
</Dir>

i have to write a transformation(xslt) to transform to text which retrieves the Path attribute of the element Dir and outputs something like this:
Dir: "C:\MyFolder\"
FileName: Test.txt

and if it sees the wildcard the text file should output as follows:
Dir:"C:\MyFolder\"
WildCard: *.txt

I know i can do a <xsl:for-each> to parse through each directory element, and use <xsl:value of select="FileName"> for retrieving element contents. i was wondering how xslt maps the choice i may have in different xml files and how to retrieve attribute Path from the <Dir> element and output in the text as described? I tried googling for this...and nobody gave a clearcut way to retrieve node attributes. Can you provide me insights into this?

thanks
 
The most clearcut way to retrieve attributes is to use XPath, in particular, check out the '@' character. For example, to select the Path attribute in my parent's element, you might consider an XPath expression something like:
Code:
../@Path

Tom Morrison
 
So you wanna use something like:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <xsl:apply-templates select="*/Dir"/>
  </xsl:template>
  <xsl:template match="Dir">
    <xsl:text>Dir: "</xsl:text>
    <xsl:value-of select="@Path"/>
    <xsl:text>"&#10;FileName: </xsl:text>
    <xsl:value-of select="FileName|WildCard"/>
    <xsl:text>&#10;&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top