data inside the xml in order to match a template

B

bjam

Hi, I am trying to have a style sheet read an xml file that has some
tagged data inside of it that has the name of the section the syle
sheet should process, does anyone know how I can do this?? Thanks
greatly for any help, I really appreciate it. Below is an example of
what I am describing.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="../xml/dqm_general.dev.xsl"?>

<perldata>
<arguments>

<test>TEST 0003</test>

<file>capacity_count.xml</file>

<xsl>dqm_general.dev.xsl</xsl>
</arguments>

Now further on in the xml document there is a tagged section called
item with a key value = "TEST 003" in the xml as such below


<item key="TEST 0003"> ... </item>

I want the style sheet to be able to read the <argument>/<test> tag in
order to know which section to match / process. I basically want the
template match and select commands to use this TEST 003 value at
processing time. Any ideas? Thanks again for your help with this.

<!-- apply templates for the test found in test tag -->

<xsl:apply-templates select='hashref/item @key="{arguments/test}"'/>

<!-- test template to process -->

<xsl:template match='hashref/item[@key={arguments/test}]'>
 
D

David Carlisle

several ways, eg:

<!-- throw items away by default -->
<xsl:template match="item"/>

<!--don't throw this one away -->
<xsl:template match="item[@key=/perldata/arguments/test]">
do something with <xsl:value-of select="@key"/>
</xsl:template>

David
 
B

Benjamin Samim

David,

Thank you so much! So simple, but was missing just not using quotes!
Thanks again as I was venturing in the <xsl:variable> tag world which
did not seem to work for me.. I was using the following.. maybe you can
comment on why this was not working? Eitherway I really appreciate the
help again. Your the best!

<xsl:template match="perldata">

<xsl:variable name="_test"
select="/perldata/arguments/test"/>

<html>
<basefont face="Verdana" size="2" />
<body>
<xsl:apply-templates select='hashref/item[@key="$_test"]'/>

</body>
</html>
</xsl:template>

<!-- template match -->

<xsl:template match='hashref/item[@key="$_test"]'>
 
D

David Carlisle

@key="$_test"

that tests if the key attribute is equal to the string $_test

You want to test if it is equal to the variable $_test so

@key=$_test

<!-- template match -->

<xsl:template match='hashref/item[@key="$_test"]'>

This is similar but you can't just remove the " here as you can't use a
variable in an XSLT1 match pattern.

But since you have already filtered the apply-templates select you can
just have

<xsl:template match='hashref/item">

David
 
B

bjam

Thanks again david. If you wanted to be able to match using a regular
expression your key value how could you do that? I have used
starts-with before
but say I have the following and I want to filter only SUMMARY sections
that have the item key="e_result" value of true (see below). Is there
a good way to do this if say the key may not always be e_result but
could be g_result?

<item key="SUMMARY">
<hashref memory_address="0x8db9dec">
<item key="non_viewable">
<hashref memory_address="0x8e46cd4">
<item key="test_value">TEST+0003</item>
</hashref>
</item>
<item key="viewable">
<hashref memory_address="0x8e511a0">
<item key="a_name">RBCNCN-0855-CITG</item>
<item key="b_capacity">3910</item>
<item key="c_lower">4297.7</item>
<item key="d_upper">3501.9</item>
<item key="e_result">true</item>
</hashref>
</item>
</hashref>
</item>
</hashref>
</item>

so my template section would be as follows

<!-- syntax is wrong here, but concept is shown of what I am trying to
do -->
<xsl:template
match="//item[@key='SUMMARY']/key(matches(_result))='true'">
 
D

David Carlisle

Thanks again david. If you wanted to be able to match using a regular

You are using XSLT2 draft? You could have said. XSLT as currently
specified doesn't have regular expressions at all. If you are using
xslt2 then the comment about variables not allowed in match patterns
doesn't apply.

<xsl:template
match="//item[@key='SUMMARY']/key(matches(_result))='true'">


There is never any point in starting a match pattern with // it never
does anytthing useful. It sometimes changes the default priority but
here it does nothing.

<xsl:template
match="item[@key='SUMMARY']//item[matches(@key,'_result')]'">

or in xslt1

<xsl:template
match="item[@key='SUMMARY']//item[contains(@key,'_result')]'">

as contains is equivalent to matches for this trivial regex of a fixed
string.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top