如何修复这个停止工作的 XSLT? [英] How do I fix this XSLT that stopped working?

查看:23
本文介绍了如何修复这个停止工作的 XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个由 Martining Honnen 慷慨提供的 XSLT(link)

I have this XSLT that Marting Honnen graciously supplied (link)

模板由于某种原因停止工作,我似乎无法修复它.数据扩大了,但我看不出这有什么关系.
而不是将双竖线分隔的文本转为xml;它只是删除分隔的数据
这是模板和示例数据:

The template stopped working for some reason and I can't seem to fix it. The data expanded but I don't see how that should matter.
Instead of turning the double pipe delimited text into xml; it just deletes the delimited data
Here's the template and sample data:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

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

<xsl:template match="str">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:analyze-string select="." regex="\|((\|[^|]+\|)+)\|">
      <xsl:matching-substring>
        <xsl:analyze-string select="regex-group(1)" regex="\|(\w+):([^|]+)\|">
          <xsl:matching-substring>
            <xsl:element name="{regex-group(1)}">
              <xsl:value-of select="regex-group(2)"/>
            </xsl:element>
          </xsl:matching-substring>
        </xsl:analyze-string>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>  

之前的数据:(注意数据已经展开)

data before: (note the data has expanded)

 <doc>
      <arr name="content">
      <str>  stream_source_info docname   stream_content_type text/html   stream_size 412   Content-Encoding ISO-8859-1   stream_name docname   Content-Type text/html; charset=ISO-8859-1   resourceName docname       ||phone:3282||email:Lori.KS@.edu||officenumber:D-107A||vcard:https://c3qa/profiles/vcard/profile.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b||photo:https://c3qa/profiles/photo.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b&amp;lastMod=1348674215846||pronunciation:https://c3qa/profiles/audio.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b&amp;lastMod=1348674215846|| background:n/a || experience:n/a || divisiongroup:11-80 || groupdesc:TII || tags:n/a ||   </str>
    </arr>
</doc> 

我可以使用 XSLT 将这个 XML 转换成这个吗?

Can I use XSLT to transform this XML into this?

 <doc>
      <arr name="content">
      <str>  stream_source_info docname   stream_content_type text/html   stream_size 412   Content-Encoding ISO-8859-1   stream_name docname   Content-Type text/html; charset=ISO-8859-1   resourceName docname  
          <phone>3282</phone>
          <email>Lori.KS@.edu</email>
          <officenumber>D-107A</officenumber>
          <vcard>https://c3qa/profiles/vcard/profile.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b</vcard>
          <photo>https://c3qa/profiles/photo.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b&amp;lastMod=1348674215846</photo>
          <pronunciation>https://c3qa/profiles/audio.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b&amp;lastMod=1348674215846</pronunciation>
          <background> ... 
      </str>
    </arr>
</doc>  

推荐答案

你的一些冒号分隔的字段有前导和尾随空格(例如 | background:n/a |),所以正则表达式需要一些调整:

Some of your colon separated fields have leading and trailing white space (e.g. | background:n/a |) so the regular expressions needs some tuning:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

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

<xsl:template match="str">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:analyze-string select="." regex="\|((\|\s*[^|]+\s*\|)+)\|">
      <xsl:matching-substring>
        <xsl:analyze-string select="regex-group(1)" regex="\|\s*(\w+):([^|]+?)\s*\|">
          <xsl:matching-substring>
            <xsl:element name="{regex-group(1)}">
              <xsl:value-of select="regex-group(2)"/>
            </xsl:element>
          </xsl:matching-substring>
        </xsl:analyze-string>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet> 

在您发布的输入 Saxon 9.5 输出上使用该样式表

Using that stylesheet on your posted input Saxon 9.5 outputs

<doc>
      <arr name="content">
      <str>  stream_source_info docname   stream_content_type text/html   stream_size 412   Content-Encoding ISO-8859-1
  stream_name docname   Content-Type text/html; charset=ISO-8859-1   resourceName docname       <phone>3282</phone>
         <email>Lori.KS@.edu</email>
         <officenumber>D-107A</officenumber>
         <vcard>https://c3qa/profiles/vcard/profile.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b</vcard>
         <photo>https://c3qa/profiles/photo.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b&amp;lastMod=1348674215846</photo>
         <pronunciation>https://c3qa/profiles/audio.do?key=5c28d263-d8aa-4a8a-ae90-4e8b13de7a0b&amp;lastMod=1348674215846</pronunciation>
         <background>n/a</background>
         <experience>n/a</experience>
         <divisiongroup>11-80</divisiongroup>
         <groupdesc>TII</groupdesc>
         <tags>n/a</tags>
      </str>
    </arr>
</doc>

这篇关于如何修复这个停止工作的 XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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