1

我是 XSLT 的新手。

我需要读取一个 URL 并将它的一些值转换为 XML。我编写了一个 XSLT,它必须将 URL 作为输入值,并从 URL 值的某些内容创建一个 XML 文件。

当我在 XMLSPY 中调试 XSLT 时,我注意到 URL 值没有被以下代码中的inputValue变量拾取。我不确定我输入 URL模板匹配的方法是否错误。

任何帮助表示赞赏。

提前致谢。

XSLT 的输入:

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

这里是 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>

所需的 XML 输出:

<?xml version="1.0" encoding="UTF-8"?>
<id>6xyz6</id>
<qq>123</qq>
<pp>123</pp>
4

1 回答 1

1

好吧,如果您想拉入文本文件,那么使用 XSLT 2.0 及更高版本您可以做到这一点,但不能简单地使用 URL,您需要调用该unparsed-text函数,例如

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

请参阅http://www.w3.org/TR/xslt20/#unparsed-text,根据您的文本文档的编码,您需要在调用函数时添加第二个参数。

于 2013-07-27T09:03:40.500 回答