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
XSLT-从cdata提取排序信息_Xslt_Xslt 1.0 - Fatal编程技术网

XSLT-从cdata提取排序信息

XSLT-从cdata提取排序信息,xslt,xslt-1.0,Xslt,Xslt 1.0,我是xslt主题的新手,有一个自己无法解决的问题。 以下是我的xml文件的示例: <node> <failure><![CDATA[ some useless information. CRS urn:ogc:def:crs:EPSG::25830 not defined. CRS urn:ogc:def:crs:EPSG::25833 not possible. CRS urn:ogc:def:crs:EPSG::25830 n

我是xslt主题的新手,有一个自己无法解决的问题。 以下是我的xml文件的示例:

<node>
  <failure><![CDATA[
    some useless information.
    CRS urn:ogc:def:crs:EPSG::25830 not defined.
    CRS urn:ogc:def:crs:EPSG::25833 not possible.
    CRS urn:ogc:def:crs:EPSG::25830 not defined. 
    some useless information.]]>
  </failure>
</node>

主要的问题是,信息位于CDATA块中,许多不同的信息混淆在一起。我找到了一种方法将它们取出,但只能作为字符串值,无法区分排序

我需要一种方法来提取符合模式的元素:“CRS[-unknown-][id]not[result]”

我想要的是这样的东西:

<failure>
    <CRS>
      <id> urn:ogc:def:crs:EPSG::25830 </id>
      <result> not defined </result>
    </CRS>
    <CRS>
      <id> urn:ogc:def:crs:EPSG::25833 </id>
      <result> not posible </result>
    </CRS>
    <CRS>
      <id> urn:ogc:def:crs:EPSG::25830 </id>
      <result> not defined </result>
    </CRS>
</failure>

urn:ogc:def:crs:EPSG::25830
未定义
urn:ogc:def:crs:EPSG::25833
不可能
urn:ogc:def:crs:EPSG::25830
未定义

有人能帮我解决类似问题吗?

XSLT 2.0有
xsl:analyze string
,它正是为这项任务而设计的,因此如果可能,我建议您升级到2.0处理器,例如:

您可以使用如下构造调用此逻辑

<xsl:template match="node">
  <failure>
    <xsl:call-template name="each-line">
      <xsl:with-param name="val" select="failure" />
    </xsl:call-template>
  </failure>
</xsl:template>


XSLT 1.0还是2.0?这在2.0中会容易得多。对不起,我忘记了这些信息。到目前为止,我使用的是XSLT 1.0。问题是我需要在普通浏览器中进行转换,因此我仅限于XSLT 1.0,或者不限于XSLT 1.0?@Logi for browser use there's,这是一个用JavaScript实现的XSLT 2.0处理器。这可能是一个解决方案。但我希望其他人有可能使用他们系统上的文件。xslt 1.0没有解决方案吗?@Logi我添加了一个递归模板的示例,您需要在纯xslt 1.0中执行此操作。很好,这几乎可以正常工作。但是输出看起来有点奇怪。一些无用信息。CRS urn:ogc:def:CRS:EPSG::25830未定义。CRS urn:ogc:def:CRS:EPSG::25833不可能。CRS urn:ogc:def:CRS:EPSG::25830未定义。一些无用信息。“和标记丢失,我无法从代码中找出原因。
<xsl:template name="each-line">
  <xsl:param name="val" />
  <!-- pull out everything before the first newline and normalize (trim leading
       and trailing whitespace and squash internal whitespace to a single space
       character -->
  <xsl:variable name="firstLine"
    select="normalize-space(substring-before($val, '&#10;'))" />
  <!-- pull out everything after the first newline -->
  <xsl:variable name="rest" select="substring-after($val, '&#10;')" />
  <xsl:if test="$firstLine">
    <xsl:call-template name="process-line">
      <xsl:with-param name="line" select="$firstLine" />
    </xsl:call-template>
  </xsl:if>
  <!-- if there are still some non-empty lines left then process them recursively -->
  <xsl:if test="normalize-space($rest)">
    <xsl:call-template name="each-line">
      <xsl:with-param name="val" select="$rest" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="process-line">
  <xsl:param name="line" />
  <xsl:if test="starts-with($line, 'CRS ') and contains($line, ' not ')">
    <!-- here $line will be something like
         "CRS urn:ogc:def:crs:EPSG::25830 not defined." -->
    <CRS>
      <!-- ID is everything between the first and second spaces -->
      <id><xsl:value-of select="substring-before(substring-after($line, ' '), ' ')" /></id>
      <!-- result is everything after the second space -->
      <result><xsl:value-of select="substring-after(substring-after($line, ' '), ' ')" /></result>
    </CRS>
  </xsl:if>
</xsl:template>
<xsl:template match="node">
  <failure>
    <xsl:call-template name="each-line">
      <xsl:with-param name="val" select="failure" />
    </xsl:call-template>
  </failure>
</xsl:template>