XSLT to select nodes dynamically

Here should go questions about transforming XML with XSLT and FOP.
adasgupta
Posts: 1
Joined: Tue Oct 09, 2018 4:21 pm

XSLT to select nodes dynamically

Post by adasgupta » Tue Oct 09, 2018 4:51 pm

I have this XML. What I want is - for each instance of <Class1> in <Class> I want to check if there is any element in <Student>/<Details> with @ClassID to be the same as <Class>/<Class1>/<Class1Data>/<ClassID>/text() and get value of <Student>/<Details>/<Details1>/<Details2>/<SomeQuantity>/text()
If there is no match, then just out a blank value.
I have been trying to create an XSLT for the same but haven't been able to make anything.

ClassID,SomeQuantity
123456,45678
789012,
345678,012345

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Class>
<Class1>
<Class1Data>
<ClassID>123456</ClassID>
</Class1Data>
</Class1>
<Class1>
<Class1Data>
<ClassID>789012</ClassID>
</Class1Data>
</Class1>
<Class1>
<Class1Data>
<ClassID>345678</ClassID>
</Class1Data>
</Class1>
</Class>
<Student>
<Details ClassID="123456">
<Details1>
<Details2>
<SomeQuantity>45678</SomeQuantity>
</Details2>
</Details1>
</Details>
<Details ClassID="123555">
<Details1>
<Details2>
<SomeQuantity>67890</SomeQuantity>
</Details2>
</Details1>
</Details>
<Details ClassID="345678">
<Details1>
<Details2>
<SomeQuantity>012345</SomeQuantity>
</Details2>
</Details1>
</Details>
</Student>
</Response>

ClassID,SomeQuantity
123456,45678
789012,
345678,012345

Martin Honnen
Posts: 52
Joined: Tue Aug 19, 2014 12:04 pm

Re: XSLT to select nodes dynamically

Post by Martin Honnen » Wed Oct 10, 2018 12:16 pm

For cross-references you can use a key, to output the data you can use value-of and/or the string concatenation operator "||" in XSLT/XPath 3:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="3.0">

<xsl:output method="text"/>

<xsl:key name="stud-ref" match="Student/Details" use="@ClassID"/>

<xsl:template match="/">
<xsl:value-of select="//ClassID!(. || ',' || key('stud-ref', .)//SomeQuantity)"
separator="&#10;"/>
</xsl:template>

</xsl:stylesheet>

Post Reply