Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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 检查xml标记中的特定值和转换-最佳实践_Java_Xml_Xslt_Batch Processing - Fatal编程技术网

Java 检查xml标记中的特定值和转换-最佳实践

Java 检查xml标记中的特定值和转换-最佳实践,java,xml,xslt,batch-processing,Java,Xml,Xslt,Batch Processing,我想做以下工作: 此时,我们收到一些xml文件,其中一些xml标记填充错误。 为了帮助我们的合作伙伴,我们希望通过使用一个“Pass-through”文件夹捕获这些错误值,在导入应用程序之前,所有xml文件都放在这个文件夹中 该文件夹每X分钟读取一次,对于每个文件,都需要进行一些检查,例如:标记内值的长度、标记的值等 因为这只是一个临时解决方案,所以我们不想在应用程序中实现它 我在考虑两种可能的设置: 使用java并调用XSLT文件来转换每个文件并将其放在另一个文件夹中 仅使用java检查xm

我想做以下工作: 此时,我们收到一些xml文件,其中一些xml标记填充错误。 为了帮助我们的合作伙伴,我们希望通过使用一个“Pass-through”文件夹捕获这些错误值,在导入应用程序之前,所有xml文件都放在这个文件夹中

该文件夹每X分钟读取一次,对于每个文件,都需要进行一些检查,例如:标记内值的长度、标记的值等

因为这只是一个临时解决方案,所以我们不想在应用程序中实现它

我在考虑两种可能的设置:

  • 使用java并调用XSLT文件来转换每个文件并将其放在另一个文件夹中
  • 仅使用java检查xml文件并进行转换。 这两种情况都将由每X分钟运行一次的.bat调用
现在我的问题是:

  • 你认为什么是最好的解决方案?a、 k.a.最快、最安全等(可能不是建议的东西?)
  • 你能不能给我举一些这样做的例子
我不像其他人那样严格要求密码。如果你能给我类似的东西,我可以自己做。 在写这篇文章的时候,我已经在其他网站上寻找解决方案,但因为这是紧急的,所以询问社区也是有帮助的

谢谢你的回答, 亲切问候,, 马丁

编辑:这两个答案对我帮助很大。谢谢大家。


如果要在给定文件夹(OP中的第一个选项)中的每个XML文件上使用.bat脚本运行XSLT,我可以想到3种方法:


A.基本上通过命令行执行“for”循环来处理每个单独的文件。(Eww)


B.使用
collection()
指向输入文件夹,并使用
xsl:result document
在新文件夹中创建输出文件

下面是一个XSLT2.0示例(使用Saxon 9测试):

XSLT2.0(使用Saxon 9测试):


注意事项:

同样,这个样式表只是在进行身份转换。它在不改变的情况下传递XML。您需要通过添加新模板来进行检查/更改,从而覆盖标识模板

还请注意,输出文件夹名称只有一个参数

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="pInputDir" select="'input'"/>
  <xsl:param name="pOutputDir" select="'output'"/>
  <xsl:variable name="vCollection" select="collection(concat($pInputDir,'/?*.xml'))"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:for-each select="$vCollection">
      <xsl:variable name="vOutFile" select="tokenize(document-uri(document(.)),'/')[last()]"/>
      <xsl:result-document href="{concat($pOutputDir,'/',$vOutFile)}">
        <xsl:apply-templates/>   
      </xsl:result-document>
    </xsl:for-each>    
  </xsl:template>

</xsl:stylesheet>
<files>
  <file>file:///C:/input_xml/file1.xml</file>
  <file>file:///C:/input_xml/file2.xml</file>
  <file>file:///C:/input_xml/file3.xml</file>
  <file>file:///C:/input_xml/file4.xml</file>
  <file>file:///C:/input_xml/file5.xml</file>
  <file>file:///C:/input_xml/file6.xml</file>
  <file>file:///C:/input_xml/file7.xml</file>
  <file>file:///C:/input_xml/file8.xml</file>
  <file>file:///C:/input_xml/file9.xml</file>
  <file>file:///C:/input_xml/file10.xml</file>
</files>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="pOutputDir" select="'output'"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="files">
    <xsl:apply-templates/>      
  </xsl:template>

  <xsl:template match="file">
    <xsl:variable name="vOutFile" select="tokenize(document-uri(document(.)),'/')[last()]"/>
    <xsl:result-document href="{concat($pOutputDir,$vOutFile)}">
      <xsl:apply-templates select="document(.)/saxon:discard-document(.)" xmlns:saxon="http://saxon.sf.net/"/>          
    </xsl:result-document>
  </xsl:template>

</xsl:stylesheet>