2013-08-01 2 views
1

У меня есть этот XSLT, что Marting Honnen любезно предоставил (link)Как исправить этот XSLT, который перестает работать?

Шаблон перестал работать по какой-то причине, и я не могу это исправить. Данные расширены, но я не вижу, как это важно.
Вместо того, чтобы превращать двойной текст с разделителями в xml; он просто удаляет разграниченные данные
Вот шаблон и образец данных:

<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> 

данные перед: (обратите внимание на данные пополнила)

<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:[email protected]||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 в этом?

<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>[email protected]</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> 

ответ

1

Некоторые из вашего двоеточия полей, разделенные имеют начальные и конечные пробела (например, | background:n/a |), поэтому регулярные выражений необходимы настройка:

<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 выходов

<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>[email protected]</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> 
+0

Вы потрясающий! Никогда не меняется! – djm

+0

Если у вас есть момент, можете ли вы взглянуть на этот новый вопрос? http://stackoverflow.com/questions/18018332/saxon9he-error-xlm0001-too-many-nested-apply-templates-calls-the-stylesheet-ma thanks – djm

Смежные вопросы