0

I have an example document that looks like this

<document>
   <memo>
       <to>Allen</to>
       <p>Hello! My name is <bold>Josh</bold></p>
       <p>It's nice to meet you <bold>Allen</bold>. I hope that we get to meet up more often.</p>
       <from>Josh</from>
   <memo>
</document>

and this is my XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:company="http://my.company">
    <xsl:output method="html"/>

    <xsl:variable name="link" select="company:generate-link()"/>

    <xsl:template match="/document/memo">
        <h1>To: <xsl:value-of select="to"/></h1>

        <xsl:for-each select="p">
            <p><xsl:apply-templates select="node()" mode="paragraph"/></p>
        </xsl:for-each>

        <xsl:if test="from">
            <p>From: <strong><xsl:value-of select="from"/></strong></p>
        </xsl:if>

        <xsl:copy-of select="$link"/>
    </xsl:template>

    <xsl:template match="bold" mode="paragraph">
        <b><xsl:value-of select="."/></b>
    </xsl:template>

    <xsl:template match="text()" mode="paragraph">
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

and the variable link contains the following example node:

<a href="#doAction"></a>

When I do a copy-of out the variable link it prints out the node correctly (but obviously without any text). I want to insert it into the document and replace the text using XSLT. For example, the text could be:

View all of <xsl:value-of select="/document/memo/from"/>'s documents.

So the resulting document would look like:

<h1>To: Allen</h1>
<p>Hello! My name is <b>Josh</b></p>
<p>It's nice to meet you <b>Allen</b>. I hope that we get to meet up more often.</p>
<from>Josh</from>
<a href="#doAction">View all of Josh's documents.</a>

I have been searching the internet on how to do this but I couldn't find anything. If anyone could help I would appreciate it a lot!

Thanks, David.

5
  • The problem is that the variable contains an XML fragment which would have to be parsed to insert the text. Can you make the variable contain only #doAction? That would make it significantly easier. Commented Dec 19, 2016 at 5:54
  • Not really - I need to specify multiple attributes (not just the href) in the link that depends on the function that gets called through generate-link(). If it's easier to set multiple attributes from one call then that might work for me as well. Commented Dec 19, 2016 at 6:01
  • In general that is a bad design. You should define the attributes you want to set and pass them in as individual values, then use those values to generate the output XML in the stylesheet. Commented Dec 19, 2016 at 6:05
  • Hi Jim, this is a helper method to make creating a link easier for users who will be defining XSLT stylesheets. Instead of having to hardcode a tag like this: <a href="#doAction" title="Find memos for Josh" target="_blank" name="find-memos" rel="noopener">View all of Josh's documents.</a> or a ton of variables within the XSLT stylesheet (these links could appear many times in a document), the helper method is there to ensure the correct output is there each time and to keep the XSLT stylesheets clean and valid. Commented Dec 19, 2016 at 6:14
  • I also want to point out that the variable has a type of Node already, it's not a slab of XML text. No parsing of the actual text is required Commented Dec 19, 2016 at 6:28

1 Answer 1

1

You haven't said which XSLT processor that is nor have you shown the code of that extension function to allow us to understand what it returns but based on your comment saying it returns a node you can usually process it further with templates so if you use <xsl:apply-templates select="$link"/> and then write a template

<xsl:template match="a[@href]">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    View all of <xsl:value-of select="$main-doc/document/memo/from"/>'s documents.
  </xsl:copy>
</xsl:template>

where you declare a global variable <xsl:variable name="main-doc" select="/"/> you should be able to transform the node returned from your function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what I was looking for. To answer your question, I am using Saxon-HE 9.7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.