Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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
Xml XSLT2.0标记化和分组_Xml_Xslt_Text_Xslt 2.0_Saxon - Fatal编程技术网

Xml XSLT2.0标记化和分组

Xml XSLT2.0标记化和分组,xml,xslt,text,xslt-2.0,saxon,Xml,Xslt,Text,Xslt 2.0,Saxon,我有一个包含以下数据的文本文件: <t>Heros Firstname Sean Lastname Connery DOB 25-08-1930 Films Dr.No 1962 Goldfinger 1964 Thunerball 1965 Award name Academy time 1 Award name BAFTA time 2 Award name Gloden Globes time 3</t> Java类: final String T

我有一个包含以下数据的文本文件:

<t>Heros
Firstname Sean
Lastname Connery
DOB 25-08-1930

Films
Dr.No 1962
Goldfinger 1964
Thunerball 1965

Award
name Academy
time 1

Award
name BAFTA
time 2

Award
name Gloden Globes
time 3</t>
Java类:

    final String TXT_PATH = "E:/tmp/test/input.txt";
    final String XSLT_PATH = "E:/tmp/test/txtToXml.xslt";
    final String XML_PATH = "E:/tmp/test/test_xml_result.xml";

    TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl();
    Transformer transformer = tFactory.newTransformer(new StreamSource(new File(XSLT_PATH)));
    transformer.transform(new StreamSource(new File(TXT_PATH)),new StreamResult(new File(XML_PATH)));
和修改的xslt:

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text(., $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{$tokens[1]}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

</xsl:stylesheet>

无效的元素名称:
无效的元素名称:

您不需要分组;您可以只标记化(以及标记化和标记化…)

这里有一个例子。它与元素名称的大小写无关。您可以在构建
$initData
期间处理这些更改,也可以添加其他模板来处理任何更改

此外,元素名称必须是有效的QName。现在样式表以消息终止处理,但您可以更改处理方式

这至少可以让你开始

XSLT2.0

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string" select="'so.txt'"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{$tokens[1]}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{replace($tokens[1],'\s','')}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/" name="root">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

</xsl:stylesheet>
Java(您需要更改路径/文件名)

XSLT2.0

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string" select="'so.txt'"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{$tokens[1]}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{replace($tokens[1],'\s','')}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/" name="root">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

</xsl:stylesheet>
同上

输出

<Jamesfilms>
   <Heros>
      <Firstname>Sean</Firstname>
      <Lastname>Connery</Lastname>
      <DOB>25-08-1930</DOB>
   </Heros>
   <Films>
      <Dr.No>1962</Dr.No>
      <Goldfinger>1964</Goldfinger>
      <Thunerball>1965</Thunerball>
   </Films>
   <Award>
      <name>Academy</name>
      <time>1</time>
   </Award>
   <Award>
      <name>BAFTA</name>
      <time>2</time>
   </Award>
   <Award>
      <name>Gloden Globes</name>
      <time>3</time>
   </Award>
</Jamesfilms>
XSLT2.0

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string" select="'so.txt'"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{$tokens[1]}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

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

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{replace($tokens[1],'\s','')}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/" name="root">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

</xsl:stylesheet>

无效的元素名称:
无效的元素名称:

输入和输出将是相同的。如果您需要我将java导入添加到示例中,请告诉我。

我已经开始使用您的解决方案,正如您所解释的,我收到了一条消息,是否有任何方法可以使元素名称必须是有效的QName?@Rembo-很高兴我能帮上忙@请留言,所以我可以改进我的so。看起来整个文本文件都被视为uri。您将
输入uri
参数设置为什么值?另外,我看到您在文本文件中添加了
t
元素(这使其成为XML文件)。真的是这样吗?如果是这样的话,我的答案就不能正常工作了。@DanielHaley from Java class
input uri
param值被设置为文本文件名。和
为了避免异常,我添加了如下内容:
文件:/E:/tmp/test/input.txt;行号:1;列数:1;prolog
@DanielHaley中不允许包含内容我已从xslt文件中删除了输入源
输入uri
,因为我是从java类传递的。请查看对我答案的编辑。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="input-encoding" as="xs:string" select="'iso-8859-1'"/>
  <xsl:param name="input-uri" as="xs:string"/>

  <xsl:variable name="initData" as="node()">
    <Jamesfilms>
      <xsl:for-each select="tokenize(unparsed-text($input-uri, $input-encoding),'\r?\n\r?\n')">
        <xsl:variable name="tokens" select="tokenize(.,'\r?\n')"/>
        <xsl:choose>
          <xsl:when test="$tokens[1] castable as xs:QName">
            <xsl:element name="{replace($tokens[1],'\s','')}">
              <xsl:for-each select="$tokens[position() > 1]">
                <xsl:variable name="tokens2" select="tokenize(.,'\s')"/>
                <xsl:choose>
                  <xsl:when test="$tokens2[1] castable as xs:QName">
                    <xsl:element name="{$tokens2[1]}">
                      <xsl:value-of select="$tokens2[position()>1]" separator=" "/>
                    </xsl:element>                      
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens2[1]"/></xsl:message>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:element>            
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">Invalid element name: <xsl:value-of select="$tokens[1]"/></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Jamesfilms>
  </xsl:variable>

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

  <xsl:template match="/" name="root">
    <xsl:apply-templates select="$initData"/>    
  </xsl:template>

  <!--Add additional templates to do further transforming of the initial data ($initData).-->

</xsl:stylesheet>