我正在尝试使用XML Includes来帮助管理需要人和机器都可用的大型XML结构.
但是在尝试构建可以针对现有模式进行验证的XML文件时遇到了无数问题.这是我正在尝试做的简化示例.
我的"main.xml"文件无法验证.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Main.xml - This fails to validate. -->
<ns1:main xsi:schemaLocation="http://www.example.com/main main.xsd"
xmlns:ns1="http://www.example.com/main"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude">
<name>String</name>
<xi:include href="child.xml"/> <!-- What I'm trying to do. -->
</ns1:main>
Run Code Online (Sandbox Code Playgroud)
"child.xml"文件可以作为独立文件进行验证.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Child.xml - This validates fine, as a standalone file. -->
<ns1:child xsi:schemaLocation="http://www.example.com/main main.xsd"
xmlns:ns1="http://www.example.com/main"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>String</name>
<age>String</age>
</ns1:child>
Run Code Online (Sandbox Code Playgroud)
这是我的架构:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Main.xsd - My Simplified Schema -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns1="http://www.example.com/main"
targetNamespace="http://www.example.com/main">
<!-- Main Element (References Child) -->
<xs:element name="main">
<xs:complexType>
<xs:sequence> …Run Code Online (Sandbox Code Playgroud) 我正在编写测试Java库的Java代码.该库包含自己的log4j2配置作为分发的一部分.
我想在我的测试代码中使用log4j2而不修改库的配置.
有没有办法为我的测试代码提供单独的log4j2配置?
这一切都作为命令行Java运行,根本没有服务器或Web参与.
编辑试图更清楚:我想要的是能够配置记录器,appender等供测试代码使用,同时让库代码使用自己独立的配置文件进行日志记录.我的想法是在我的测试代码中使用log4j2,但无需更改库的配置文件.由于库配置文件是库分发的一部分,我不想更改它以进行测试.
我正在尝试使用xinclude将xml文件解组到Java对象。我有一个基于jaxb注释的代码文件的架构。这是我的解组代码:
@Override
public T readFromReader(final Reader reader) throws Exception {
final Unmarshaller unmarshaller = createUnmarshaller();
final SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setXIncludeAware(true);
spf.setNamespaceAware(true);
//spf.setValidating(true);
final XMLReader xr = spf.newSAXParser().getXMLReader();
final InputSource inputSource = new InputSource(reader);
xr.parse(inputSource)
final SAXSource source = new SAXSource( xr, new InputSource(reader) );
try {
final T object = (T) unmarshaller.unmarshal(source);
postReadSetup(object);
return object;
} catch (final Exception e) {
throw new RuntimeException("Cannot parse XML: Additional information is attached. Please ensure your XML is valid.", e);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个xml文档,使用xinclude访问其他几个xml文件.
<chapter xml:id="chapter1">
<title>Chapter in Main Doc</title>
<section xml:id="section">
<title>Section in Main Doc 1</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/car.jpg"/>
</imageobject>
</mediaobject>
</section>
<xi:include href="../some-doc/section1.xml"/>
<xi:include href="../some-doc/section2.xml"/>
Run Code Online (Sandbox Code Playgroud)
这些其他section1和section2 xml文件在不同的源位置使用不同的图像.我需要将所有图像复制到单个输出目录.首先,我计划使用XSLT来解析整个xml文档并生成要复制的图像列表.如何使用XSLT生成xml文件的图像列表?你的想法真的很感激.
提前致谢..!!
添加:
我尝试了下面回答的XSLT 1.0代码.当我使用它生成html输出时,它只显示章节和节id,如"chapter1,section ...".它不显示imagedata节点内的图像路径值.
但是,当我改变<xsl:template match="@*|node()">作为<xsl:template match="*">然后显示的xincluded XML文件也全部图像路径值.但是还有其他节点的值也如上所述.我需要过滤除图像路径以外的所有值.
在这里,我只需要复制所有xml文档的图像路径,并将所有路径保存在数组或类似的东西中.然后我可以使用这些保存的图像路径来使用java类进行图像处理.