Using a for-each, can I get the value of an array item?

72 views Asked by At

I'm fairly new to XSL, so perhaps my concept of how the for-each works is incorrect. My for-each is grabbing each item in the XML document from the supplied url's. This is working as intended. What I can't seem to figure out is how to take the url associated with the respective xml document from the array, and concat it with the href property from the item. Is that possible? What would add I add to the area marked as WHAT_GOES_HERE?

For reference, this is XSL 2.0

    <xsl:variable name="array" as="element()*">
        <item>https://www.example.com/_resources/data/blogs.xml</item> <!--item 1-->
        <item>https://www.example.com/_resources/data/blogs2.xml</item> <!--item 2--> 
    </xsl:variable>

    <xsl:for-each select="$array/document(.)/items/item">
       <item>
           <xsl:attribute name="href" select="concat(WHAT_GOES_HERE?,@href)"/>
       </item>
     </xsl:for-each>

Here is an XML example

    <items>
<item href="/news-and-events/news/2024-02-amplify-stem-course-based-undergraduate-research-experience.aspx" dsn="blogs">
      <homehero>true</homehero>
      <pubDate>02/19/2024</pubDate>
      <title>Example TItle</title>
      <description>Example</description>
      <highlights/>
      <author>Author Name</author>
      <hero-image>
        <img src="" alt=""/>
      </hero-image>
      <image>
        <img src="/_assets/images/news/2024/jinjie-square.JPG" alt="Four women look at computers in a lab"/>
      </image>
      <tags>
        <tag>lab</tag>
      </tags>
    <searchtags>lab</searchtags>
    </item>
</items>

Ultimately what I want is:

select="concat(https://www.example.com/_resources/data/blogs.xml',@href)"
select="concat('https://www.example.com/_resources/data/blogs2.xml',@href)"

How do I get those values in there from the array?

2

There are 2 answers

0
y.arazim On

I think you need to do:

<xsl:for-each select="$array">
    <xsl:variable name="array-item" select="." />
    <xsl:for-each select="document(.)/items/item">
       <item>
           <xsl:attribute name="href" select="concat($array-item, @href)"/>
       </item>
    </xsl:for-each>
</xsl:for-each>

or more elegantly:

<xsl:for-each select="$array">
    <xsl:variable name="array-item" select="." />
    <xsl:for-each select="document($array-item)/items/item">
       <item href="{$array-item}{@href}"/>
    </xsl:for-each>
</xsl:for-each>

But the results do not make much sense to me.

0
Martin Honnen On

I think you want something like

<xsl:for-each select="$array/document(.)/items/item">
   <item href="{base-uri(.)}{@href}"/>
 </xsl:for-each>