1
votes

I have got a xml file and xsl file linking to it,that works fine.Im trying to figure out a way where I can dynamically pull values from xml rather than writing

xsl:value-of select="tag..."

each time I add a new node in the caseStudy tag. Im including the xml and xsl files below.

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="caseStudies.xsl"?>

<caseStudiesList>
    <caseStudy>
        <tag1 label="tag1">TAG1_value</tag1>
        <tag2 label="tag2">TAG2_value</tag2>
        <tag3 label="tag3">TAG3_value</tag3>
        <tag4 label="tag4">TAG4_value</tag4>
        <tag5 label="tag5">TAG5_value</tag5>
    </caseStudy>
    <caseStudy>
        <tag1 label="tag1">TAG1_value</tag1>
        <tag2 label="tag2">TAG2_value</tag2>
        <tag3 label="tag3">TAG3_value</tag3>
    </caseStudy>
    <caseStudy>
        <tag1 label="tag1">TAG1_value</tag1>
        <tag2 label="tag2">TAG2_value</tag2>
        <tag3 label="tag3">TAG3_value</tag3>
        <tag4 label="tag4">TAG4_value</tag4>
        <tag5 label="tag5">TAG5_value</tag5>
        <tag6 label="tag6">TAG6_value</tag6>
        <tag7 label="tag7">TAG7_value</tag7>
    </caseStudy>
</caseStudiesList>

XSL

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
            <xsl:for-each select="caseStudiesList/caseStudy">
                <div class="caseStudyContainer ">
                    <ul>
                        <li>
                            <span><xsl:value-of select="tag1/@label" /></span>
                            <xsl:value-of select="tag1" />                          
                        </li>
                        <li>
                            <span><xsl:value-of select="tag2/@label" /></span>
                            <xsl:value-of select="tag2" />                          
                        </li>
                        <li>
                            <span><xsl:value-of select="tag3/@label" /></span>
                            <xsl:value-of select="tag3" />                          
                        </li>
                        <li>
                            <span><xsl:value-of select="tag4/@label" /></span>
                            <xsl:value-of select="tag4" />                          
                        </li>
                        <li>
                            <span><xsl:value-of select="tag5/@label" /></span>
                            <xsl:value-of select="tag5" />                          
                        </li>
                        <li>
                            <span><xsl:value-of select="tag6/@label" /></span>
                            <xsl:value-of select="tag6" />                          
                        </li>
                        <li>
                            <span><xsl:value-of select="tag7/@label" /></span>
                            <xsl:value-of select="tag7" />                          
                        </li>
                    </ul>
                </div>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

Hope Im making some sense.Is there any way of getting the values dynamically in XSL from the XML file?

Thank You

2

2 Answers

3
votes

You just need to nest another for-each...

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
    <head>
    </head>
    <body>
        <xsl:for-each select="caseStudiesList/caseStudy">
        <div class="caseStudyContainer ">
            <ul>
              <xsl:for-each select="*">
                <li>
                    <span><xsl:value-of select="@label" /></span>
                    <xsl:value-of select="." />                          
                </li>
              </xsl:for-each>
            </ul>
        </div>
        </xsl:for-each>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>
0
votes

As a rule in XML, it is not recommended to use different tag names to refer to the same kind of data. This is one situation where that rule really comes into the picture. You already know how to use for-each so why not apply it here?

 <xsl:for-each select="caseStudiesList/caseStudy">
                <div class="caseStudyContainer ">
                    <ul><xsl:for-each select="tag"><li><span><xsl:value-of select="@label" /></span><xsl:value-of select="." /></li>
                       </xsl:for-each></ul>
</div>
</xsl:for-each>

Just call each of the individual elements tag instead of tag1 tag2 etc. And, well, consider a more sensible name than "tag".