1

I am newbie to XSLT.

I have a requirement to read a URL and convert some of its values into XML. I wrote a XSLT that has to take URL as the input value and create a XML file from some of the content of the URL value.

When I debugged the XSLT in XMLSPY, I noticed that the URL value is not being picked up by inputValue variable in the below code. I am not sure if my approach to input the URL and the template match are wrong.

Any help is appreciated.

Thanks in advance.

Input to XSLT:

http://host:port/abc/xyz1/6xyz6?qq=123&pp=3

Here the XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:nnc="Nnc" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:param name="inVal" select="xs:string(http://host:port/abc/xyz1/6xyz6?qq=123&pp=3)"/>
    <xsl:template match="/">
        <xsl:variable name="inputValue" select="$inVal"/>
            <xsl:if test="string-length($inputValue)=0">
                <xsl:message terminate="yes">
                inputValue is blank
                </xsl:message>
            </xsl:if>
            <xsl:variable name="value" as="xs:string" select="substring-after($inputValue, 'abc/' )"/>
            <xsl:variable name="tokenizedValues" select="tokenize($value,'/')"/>
            <xsl:for-each select="$tokenizedValues">
                <xsl:if test="position() = 1">
                    <id>
                        <xsl:value-of select="."/>
                    </id>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
</xsl:stylesheet>

The desired XML output:

<?xml version="1.0" encoding="UTF-8"?>
<id>6xyz6</id>
<qq>123</qq>
<pp>123</pp>
5
  • What is your current result? If I'm not mistaken, the input to an XSLT has to be an XML document (even if it's just a single empty element). So your input could be something llke <url>http://host:port/abc/xyz1/6xyz6?qq=123&pp=3</url>, and you could just access it in that template as .. Commented Jul 27, 2013 at 2:04
  • Currently the result is empty as inputValue is blank. I am using XSLT 2.0 which as per documentation would allow NON-XML data as input... Commented Jul 27, 2013 at 2:21
  • Ok, well $inVal is a parameter, so unless you are passing your value in as a parameter, I don't think $inVal will have the value you want. How about just trying . to get the input value, as in: <xsl:value-of select="." />? Commented Jul 27, 2013 at 3:35
  • Did you mean change this <xsl:template match="/"> <xsl:variable name="inputValue" select="$inVal"/> to <xsl:template match="/"> <xsl:variable name="inputValue" select="."/>. If so, the inputValue is having the entire xslt as a document instead of the url as the value. Sorry for the confusion as I am a newbie - code example is appreciated. Thanks! Commented Jul 27, 2013 at 7:24
  • With XSLT 2.0, if you use a primary input document and template match="/", you need to feed an XML document. You can however read in a text document as secondary input as I have shown in my answer and if you don't have and don't want an XML input as well you can start the transformation with a named template (e.g. <xsl:template name="main"><xsl:variable name="inputData" select="unparsed-text('http://example.com/foo')/>...</xsl:template>), you only need to make sure you use the proper options to start processing with that template (with Saxon 9 from the cmmd line -it:main). Commented Jul 28, 2013 at 12:06

1 Answer 1

1

Well if you want to pull in a text file then with XSLT 2.0 and later you can do that but not by simply using a URL, you need to call the unparsed-text function e.g.

<xsl:variable name="inputData" as="xs:string" select="unparsed-text('http://example.com/foo')"/>

See http://www.w3.org/TR/xslt20/#unparsed-text, depending on the encoding of your text document you need to add a second parameter when calling the function.

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

Comments

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.