Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中从字符串处理XSLT源时使用键进行XSLT查找_Xslt - Fatal编程技术网

在java中从字符串处理XSLT源时使用键进行XSLT查找

在java中从字符串处理XSLT源时使用键进行XSLT查找,xslt,Xslt,我正在尝试处理XSLT查找—在经过某些修改后,将输入XSLT作为字符串而不是文件。如果我将修改后的XSLT作为一个文件编写并再次读取以进行转换,那么一切都很好,但是当我将其作为一个字符串处理时,查找不起作用 进行转换的方法如下所示 public static void transform(File inputXmlfile, String outputXmlFileName) throws ParserConfigurationException, SAXException, IOExc

我正在尝试处理XSLT查找—在经过某些修改后,将输入XSLT作为字符串而不是文件。如果我将修改后的XSLT作为一个文件编写并再次读取以进行转换,那么一切都很好,但是当我将其作为一个字符串处理时,查找不起作用

进行转换的方法如下所示

    public static void transform(File inputXmlfile, String outputXmlFileName) throws ParserConfigurationException, SAXException, IOException, TransformerException {

    DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = docBuildFactory.newDocumentBuilder();
    Document document = parser.parse(inputXmlfile);

    TransformerFactory xformFactory = TransformerFactory.newInstance();

    String xsltString="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
    "<xsl:stylesheet version=\"1.0\""+
    " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:c=\"http://myDomain.com/classifications.data\">"+
    "<xsl:output method=\"xml\" />"+
    "<xsl:key name=\"classification-lookup\" match=\"c:classification\" use=\"c:id\" /> <xsl:template match=\"/\"><listings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://local.google.com/local_feed.xsd\"><language>en</language><datum>WGS84</datum>" +
    "<xsl:for-each select=\"BusinessListings/BusinessListing\"><listing><id><xsl:value-of select=\"id\" /></id><xsl:apply-templates /></listing></xsl:for-each></listings></xsl:template><xsl:template match=\"classificationId\"><xsl:variable name=\"currentId\" select=\".\" />" +
    "<xsl:for-each select=\"document('')\"><category><xsl:value-of select=\"key('classification-lookup',$currentId)/c:description\" /></category></xsl:for-each></xsl:template> <xsl:template match=\"text()\" />" +
    "<c:classifications><c:classification><c:id>3</c:id><c:description>Abortion Alternatives</c:description></c:classification><c:classification><c:id>4</c:id><c:description>Abortion Providers</c:description>" +
    "</c:classification><c:classification><c:id>9</c:id><c:description>Abrasives</c:description></c:classification></c:classifications></xsl:stylesheet>";

    Transformer transformer = xformFactory.newTransformer(new StreamSource(IOUtils.toInputStream(xsltString)));
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    FileOutputStream fileOutputStream = new FileOutputStream(new File(outputXmlFileName));  
    DOMSource source = new DOMSource(document);
    Result result = new StreamResult(fileOutputStream);
    transformer.transform(source, result);

    fileOutputStream.close();

}
它工作得很好,但是

    Transformer transformer = xformFactory.newTransformer(new StreamSource(IOUtils.toInputStream(xsltString))); does not process the lookup.
输入如下

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BusinessListings vendorId="0" schemaVersion=""
fileCreateDate="" xmlns="http://www.myDomain.com">
<BusinessListing>
    <id>1593469</id>
    <listingData>
        <classifications>
            <classificationId>3</classificationId>
            <classificationId>9</classificationId>
        </classifications>
    </listingData>
</BusinessListing>
</BusinessListings>

1593469
3.
9

问题可能是什么?

能否将ByteArrayInputStream传递到StreamSource对象中?你可以用一个字符串初始化它

比如:

ByteArrayInputStream bais = new ByteArrayInputStream(xsltString.getBytes(), "UTF-8");
Transformer transformer = xformFactory.newTransformer( new StreamSource( bais ) );

首先,要回答您的问题,请参考XSLT 1.0规范,该规范解释了零长度字符串arg的用法:

注意,零长度URI引用是对URI引用正被解析的文档的引用;因此,document(“”)是指样式表的根节点;样式表的树表示形式与包含样式表的XML文档是初始源文档的情况完全相同

因为样式表的源是内存中的流,所以没有URI可以解析它。它与文件一起工作,因为它能够解析文件的位置并重新加载文件。可能有一些方法可以让它与系统id或自定义URI解析器一起工作,但我认为可能有一种更简单的方法


第二,为什么要在XSLT中嵌入分类数据?为什么不把它作为一个参数传进来呢?用对参数的引用替换文档(“”)调用似乎容易得多,而且基本上也可以这样做。

ByteArrayInputStream似乎没有一个接受字符串参数的构造函数。我尝试了ByteArrayInputStream bais=new ByteArrayInputStream(xsltString.getBytes());-没有任何运气,我想这就是原因。
ByteArrayInputStream bais = new ByteArrayInputStream(xsltString.getBytes(), "UTF-8");
Transformer transformer = xformFactory.newTransformer( new StreamSource( bais ) );