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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want the attribute name to be variable...

Status
Not open for further replies.

zalim30

Programmer
Oct 13, 2005
17
GB
hi I am doing the following

<xsl:variable name ="att" select = "//@partid" />

i.e. selecting all attributes having 'partid' name. what I want to do is that this partid is variable. i.e. something that can change. i.e.

<xsl:variable name ="att" select = "//@XXXX" />

where XXXX is a variable....
how to do this...[i.e. I don't want to fix the attribute name to partid...but want it to be a variable]
 
Where do you want the value to come from then? @partid can change in the XML, thus changing the variable.

Jon

"I don't regret this, but I both rue and lament it.
 
sorry probably I could not explain the problem...
see, I am using

@partid and thus it selects partid attributes...
but I want it to be generic such that...

@xyz and xyz is some variable which I can set
thus if xyz is partid then it would become @partid
and if xyz is ID then it would become @id

I hope i made it clear this time. thanx for the first response.
 
Ah, I get you now.
Code:
<xsl:variable name ="att">
  <xsl:value-of select="//@*[name(.) = 'partid']"/>
</xsl:variable>
Obviously you can insert a variable where 'partid' is if you so wish. Avoid using // where you can cos its very inefficient, specify the full path.

Jon

"I don't regret this, but I both rue and lament it.
 
hi jonty
thank you very much for your reply. Can you see this in little bit more detail that what I am trying to do is:

<xsl:variable name ="attribute1"
select = "document($file2)//@ID"/>
<xsl:copy-of
select="document($file1)//*[@partid=$attribute1]"/>

It is a simple two step process.
1- select all attribues named 'ID' from file 1
2- copy all elements from file2 whose attribute named 'partid' matches any of the attributes of file1.

This is working fine. The only problem is that it is not generic...what I want is that instead of @ID I could do @xxxx and instead of @partid I could do @yyyy.
I am trying your previous solution as well. But if you could figure it out I would be thankful.
 
hi jonty
<xsl:variable name ="attribute1"
select = "document($file2)//@*[name(.)=$param2]"/>
<xsl:copy-of
select=
"document($file1)//*[@*[name(.)=$param1]=$attribute1]"/>

I am doing the above as you said....but it throws unkown XPath Error???
 
Your syntax is exactly right. Does $file1 point to file and is string? Is $param1 string? Post your full XML file(s).

Bear in mind that your variable here will only contain the value of the first atttribute whose name = $param2.

Jon

"I don't regret this, but I both rue and lament it.
 
hi Jonty...thanks again for sorting this out..below are all my input files and xslts. [this is going to be a long post]

------------------first.xml----------------------
<?xml version="1.0"?>
<root1>
<object partid ="4" name = "def" price = "3000"/>
<object partid ="5" name = "jkl" price = "130"/>
<object partid ="1" name = "abc1" price = "0"/>
<object partid ="2" name = "def" price = "430"/>
<object partid ="3" name = "abc" price = "350"/>
<object partid ="4" name = "abc" price = "230"/>
</root1>



----------------second.xml-----------------------
<?xml version="1.0"?>
<root2>
<object ID ="2" name = "def" price = "430"/>
<object ID ="4" name = "defijk" price = "3000"/>
<object ID ="50" name = "jkl" price = "130"/>
<object ID ="1" name = "abc" price = "0"/>
<object ID ="7" name = "abc" price = "2"/>
<object ID ="17" name = "xyz" price = "2"/>
</root2>

-----------------input1.xml----------------------
<?xml version="1.0"?>

<input>
<file1>first.xml</file1>
<param1>partid</param1>
<file2>second.xml</file2>
<param2>ID</param2>
</input>

----------------test2.xslt-------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="

<xsl:variable name="file1" select="//file1" />
<xsl:variable name="file2" select="//file2" />
<xsl:variable name="param2" select="//param2" />
<xsl:variable name="param1" select="//param1" />


<xsl:template match="/">
<root>


<xsl:variable name ="attribute1" select = "document($file2)//@ID"/>
<xsl:copy-of select="document($file1)//*[@partid=$attribute1]"/>


</root>


</xsl:template>


</xsl:stylesheet>

--------------------result.xml-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<root>
<object partid="4" name="def" price="3000"/>
<object partid="1" name="abc1" price="0"/>
<object partid="2" name="def" price="430"/>
<object partid="4" name="abc" price="230"/>
</root>

As you can see the result is fine...only matching nodes are copies. I am using xalan parser and

following is the command that I use to run the xslt engine...
java org.apache.xalan.xslt.Process -in input1.xml -xsl test2.xsl -out result.xml

What i am doing is that I am processing input1.xml which contains the filenames for the two files

and also the attribute names. Then using the xslt...I am opening these files and copying the

content using the following two lines

<xsl:variable name ="attribute1" select = "document($file2)//@ID"/>
<xsl:copy-of select="document($file1)//*[@partid=$attribute1]"/>

but when I try to make the attribute name generic by replacing the above two lines with this....



<xsl:variable name ="attribute1" select = "document($file2)//@*[name(.)=$param2]"/>
<xsl:copy-of select="document($file1)//*[@*[name(.)=$param1]=$attribute1]"/>

it gives an xpath error...
my new xslt is
-------------------------------------------------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="

<xsl:variable name="file1" select="//file1" />
<xsl:variable name="file2" select="//file2" />
<xsl:variable name="param2" select="//param2" />
<xsl:variable name="param1" select="//param1" />


<xsl:template match="/">
<root>



<xsl:variable name ="attribute1" select = "document($file2)//@*[name(.)=$param2]"/>
<xsl:copy-of select="document($file1)//*[@*[name(.)=$param1]=$attribute1]"/>

</root>


</xsl:template>


</xsl:stylesheet>

What is wrong here...even I try to delcare a simple variable and try to use it...still could not

get it.
Thank you for your earlier responses and I will wait for this one as well. Plus, if you any good

idea about solving this problem i.e. taking the intersection of two xml files bases on some

attributes and similarly doing other operations such as taking union of two xml files...I would

really appreciate that. There is a limitation as well...because we will be working on very large

sets of data...millions of records...so we need a memory efficient solution.
Thanks jonty.
 
hi jonty
It worked out...thanks. Actually I changed the input file format a little bit as

<?xml version="1.0"?>

<input>
<file1 finename="first.xml" paramname="partid"</file1>
<file2 finename="second.xml" paramname="ID"</file1>
</input>

and changed
<xsl:variable name="file1" select="//file1/@filename" />
<xsl:variable name="file2" select="//file2/@filename" />
<xsl:variable name="param2" select="//file1/@paramname" />
<xsl:variable name="param1" select="//file2/@paramname" />

and it worked fine...I think when I selected //file1 ... it did not give me back string...so there was this problem...Anyways thanks alot for rendering your help...There might be more problems coming soon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top