XSLT遍历目录中的一组文件吗? [英] XSLT loop over a set of files in the directory?

查看:54
本文介绍了XSLT遍历目录中的一组文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我的目录中充满了xsd文件,需要对其进行转换,从而为每个文件生成一个输出文件.我的样式表可以在单个文档上运行,但我想对此进行扩展.好吧,目前我还没有使用xslt编辑器,因此已经安装了saxon.这是xslt文件:

 < xsl:stylesheet version ="1.0"xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"xmlns:xsd ="http://www.w3.org/2001/XMLSchema">< xsl:output method ="text"/><!-*忽略看起来复杂的任何内容*->< xsl:template match ="xsd:attribute|xsd:attributeGroup|xsd:group|xsd:schema/xsd:element [@type]|xsd:符号|xsd:annotation"/><!-*忽略文本节点(否则输出将是*被空格淹没)*->< xsl:template match ="text()"/><!-*具有局部复杂类型的顶级元素;那些*我们要处理.*->< xsl:template match ="xsd:schema/xsd:element [xsd:complexType]">< xsl:apply-templates/></xsl:template><!-*啊哈!我们要转换其内容模型的复杂类型*转换为正则表达式*->< xsl:template match ="xsd:element/xsd:complexType[xsd:sequence |xsd:choice |xsd:all]><!-*写出命名正则表达式的名称*->< xsl:value-of select ="concat('& #xA;& #xA;',@name,父级:: xsd:element/@ name,'')"/><!-*写出正则表达式*->< xsl:apply-templates/></xsl:template><!-*简单的递归案例:我们遇到一个模型组.*->< xsl:template match ="xsd:sequence | xsd:choice | xsd:all"><!-*给组加上括号并处理其子级.*->< xsl:text>(</xsl:text>< xsl:apply-templates/>< xsl:text>)</xsl:text><!-*附加*,?,+或{min,max}.*->< xsl:call-template name ="occurrence-indicator"/><!-*如果我们的父母还有孩子,*附加适当的连接器.*->< xsl:call-template name ="connector"/></xsl:template><!-*内容模型中的元素.*->< xsl:template match ="xsd:element [ancestor :: xsd:complexType]"><!-*写出元素的名称.我们很懒,所以*我们不必为本地元素使用QName.*另外,我们不会重复发生.*->< xsl:value-of select ="concat(@ref,@name)"/><!-*处理出现指示器并连接*和团体一样.*->< xsl:call-template name ="occurrence-indicator"/>< xsl:call-template name ="connector"/></xsl:template><!-*为*组或元素.*使用{min,max},{min,}或{n}表示法*非标准出现次数.*->< xsl:template name ="occurrence-indicator">< xsl:choose>< xsl:when test =(@ minOccurs ='1'或否(@minOccurs))和(@ maxOccurs ='1'或否(@maxOccurs))>< xsl:text></xsl:text></xsl:when>< xsl:when test ="@ minOccurs ='0'和(@ maxOccurs ='1'或否(@maxOccurs))>< xsl:text>?</xsl:text></xsl:when>< xsl:when test ="@ minOccurs ='0'和@ maxOccurs ='unbounded'">< xsl:text> *</xsl:text></xsl:when>< xsl:when test =(@ minOccurs ='1'或否(@minOccurs))和@ maxOccurs ='unbounded'>< xsl:text> +</xsl:text></xsl:when>< xsl:when test ="@ minOccurs = @ maxOccurs">< xsl:value-of select ="concat('{',@minOccurs,'}')"/></xsl:when>< xsl:when test ="@ maxOccurs ='unbounded'">< xsl:value-of select ="concat('{',@minOccurs,',}')"/></xsl:when>< xsl:otherwise>< xsl:value-of select ="concat('{',@minOccurs,',',@maxOccurs,'}')"/></xsl:otherwise></xsl:choose></xsl:template>< xsl:template name ="connector"><!-*如果需要,请发出适当的连接器.*->< xsl:if test ="following-sibling :: * [self :: xsd:sequence|自我:: xsd:选择|自我:: xsd:全部|self :: xsd:element]>< xsl:choose>< xsl:when test ="parent :: xsd:sequence">< xsl:text> ;,</xsl:text></xsl:when>< xsl:when test ="parent :: xsd:choice">< xsl:text>|</xsl:text></xsl:when>< xsl:when test ="parent :: xsd:all">< xsl:text>& amp;</xsl:text></xsl:when></xsl:choose></xsl:if></xsl:template></xsl:stylesheet> 

解决方案

进行匹配处理的方法有很多(bash,vb-scrpt等).

我经常使用 ant .这里有一些示例如何将XSLT应用于文件夹中的多个文件.对"destinationFolder"中所有.xsd文件运行XSL转换的ant"build.xml"文件:

 <?xml version ="1.0" encoding ="UTF-8"?>< project name ="TransformMultipleFiles" default ="transformMulti"><属性名称="xsl_processor.file"值="saxon9he.jar"/>< target name ="transformMulti"><!-转换目录中的所有文件->< xslt basedir ="fileFolder" destdir ="destinationFolder" includes ="**/*.xsd" extension =.xml" style ="yourXSLT.xslt" classpath ="$ {xsl_processor.file}""/></target></project> 

只需将以上代码添加到一个名为build.xml的文件中,并通过进入该目录并在控制台中键入"ant",使用ant运行该文件.(当然必须安装ant-并设置环境变量).由于ant是Java,因此您可以在任何系统上运行它: http://ant.apache.org/

或者您可以使用 collection()函数,但是我不确定它是否适用于所有XSLT处理器.在此处查看示例: http://www.xmlplease.com/collection

I have a situation where I have a directory full of xsd files that need conversion done to them generate a output file for each of them. I have my stylesheet operating on a single document fine, but I'd like to extend that. Well, for now I haven't using a xslt editor, saxon has installed. here is xslt file:

<xsl:stylesheet version="1.0" 
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            >

<xsl:output method="text"/>

<!--* Ignore anything that looks complicated *-->
<xsl:template match="xsd:attribute 
                   | xsd:attributeGroup
                   | xsd:group
                   | xsd:schema/xsd:element[@type]
                   | xsd:notation
                   | xsd:annotation
                   "/>
<!--* Ignore text nodes (otherwise the output will be
  * inundated with whitespace) *-->

<xsl:template match="text()"/>

 <!--* Top-level elements with local complex types; those
  * we want to handle.
  *-->

<xsl:template match = "xsd:schema/xsd:element[xsd:complexType]">
<xsl:apply-templates/>
  </xsl:template>

  <!--* Aha!  A complex type whose content model we want to turn 
  * into a regular expression 
  *-->

<xsl:template match = "xsd:element/xsd:complexType
                     [xsd:sequence | xsd:choice | xsd:all]">
<!--* write out the name for the named regex *-->
<xsl:value-of select="concat('&#xA;&#xA;',
                      @name, parent::xsd:element/@name, 
                      ' ')"/>
<!--* write out the regex *-->
<xsl:apply-templates/>
</xsl:template>

<!--* Simple recursive case:  we encounter a model group. *-->

<xsl:template match = "xsd:sequence|xsd:choice|xsd:all">
<!--* Parenthesize the group and handle its children. *-->
<xsl:text>(</xsl:text>
<xsl:apply-templates/>
<xsl:text>)</xsl:text>

<!--* Append *, ?, +, or {min, max}. *-->
<xsl:call-template name="occurrence-indicator"/>

<!--* If our parent has further children, 
    * append the appropriate connector. *-->
<xsl:call-template name="connector"/>
</xsl:template>

<!--* An element in a content model. *-->
  <xsl:template match = "xsd:element[ancestor::xsd:complexType]">
<!--* Write out the element's name.  We're lazy so 
    * we don't bother with a QName for a local element.
    * Also, we don't recur. *-->
<xsl:value-of select="concat(@ref, @name)"/>

<!--* Handle occurrence indicator and connect
    * just as for groups. *-->
<xsl:call-template name="occurrence-indicator"/>
<xsl:call-template name="connector"/>
</xsl:template>


<!--* Emit the appropriate occurrence indicator for
  * a group or element.
  * Use {min,max}, {min,}, or {n} notation for 
  * non-standard occurrence counts.
  *-->

<xsl:template name="occurrence-indicator">
<xsl:choose>
  <xsl:when test="(@minOccurs='1' or not(@minOccurs)) 
                  and 
                  (@maxOccurs='1' or not(@maxOccurs))">
    <xsl:text></xsl:text>
  </xsl:when>
  <xsl:when test="@minOccurs='0' 
                  and 
                  (@maxOccurs='1' or not(@maxOccurs))">
    <xsl:text>?</xsl:text>
  </xsl:when>
  <xsl:when test="@minOccurs='0' and @maxOccurs='unbounded'">
    <xsl:text>*</xsl:text>
  </xsl:when>
  <xsl:when test="(@minOccurs='1' or not(@minOccurs)) 
                  and 
                  @maxOccurs='unbounded'">
    <xsl:text>+</xsl:text>
  </xsl:when>
  <xsl:when test="@minOccurs=@maxOccurs">
    <xsl:value-of select="concat('{', @minOccurs,'}')"/>
  </xsl:when>
  <xsl:when test="@maxOccurs='unbounded'">
    <xsl:value-of select="concat('{', @minOccurs,',}')"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="concat('{', 
                          @minOccurs,
                          ',',
                          @maxOccurs,
                          '}')"/>
  </xsl:otherwise>
</xsl:choose>
  </xsl:template>

  <xsl:template name="connector">
<!--* Emit the appropriate connector, if we need one. *-->
<xsl:if test="following-sibling::*[self::xsd:sequence 
              | self::xsd:choice 
              | self::xsd:all 
              | self::xsd:element]">
  <xsl:choose>
    <xsl:when test="parent::xsd:sequence">
      <xsl:text>, </xsl:text>
    </xsl:when>
    <xsl:when test="parent::xsd:choice">
      <xsl:text> | </xsl:text>
    </xsl:when>
    <xsl:when test="parent::xsd:all">
      <xsl:text> &amp; </xsl:text>
    </xsl:when>
  </xsl:choose>
</xsl:if>
  </xsl:template>

</xsl:stylesheet>

解决方案

There are so many ways to do match treatment (bash, vb-scrpt, ...).

I often use ant. Here some examples how to apply XSLT on multiple files in a folder. The ant "build.xml" file running the XSL transformation on all .xsd files in the "destinationFolder":

<?xml version="1.0" encoding="UTF-8"?>
<project name="TransformMultipleFiles" default="transformMulti">
   <property name="xsl_processor.file" value="saxon9he.jar"/>
   <target name="transformMulti">
    <!-- Transform all the files in the directory -->
       <xslt basedir="fileFolder" destdir="destinationFolder" includes="**/*.xsd" extension=".xml" style="yourXSLT.xslt" classpath="${xsl_processor.file}" />
   </target>
</project>

Just add above code into a file named build.xml and run the file with ant by just going into this directory and typing "ant" into the console. (ant must be installed of course - and the environment variables set). as ant is Java you can run it on any system: http://ant.apache.org/

Or you can use the collection() function, but I'm not sure if it works with all XSLT processors. See examples here: http://www.xmlplease.com/collection

这篇关于XSLT遍历目录中的一组文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆