具有属性的xsl正则表达式识别失败

时间:2014-02-24 13:33:29

标签: xml regex xslt

这是我的xml源代码。如您所见,它没有常规结构 - 请参阅此处的“auteur_5f_nom”

<?xml version="1.0" encoding="UTF-8"?>
<texte>
 <Standard/>
 <Standard/>
 <Standard>
  <auteur_5f_nom>auteur_nom XXX XXX </auteur_5f_nom>
  <auteur_5f_prenom>auteur_prenom XXX </auteur_5f_prenom>
  <date_5f_action>date_action 00/00/00 </date_5f_action>
  <date_5f_redaction>date_redaction 00/00/00 </date_5f_redaction>
  <mail/>
  <texteID>TexteID </texteID>
  <texteID>1234 </texteID>
   01 
 </Standard>
</texte>

...稍后在文档源中,“auteur_5f_nom”标记:

</Standard>
<Standard/>
<Standard>
 <auteur_5f_nom>auteur_nom </auteur_5f_nom>
 <auteur_5f_nom>XXX </auteur_5f_nom>
 <auteur_5f_nom>auteur_prenom </auteur_5f_nom>
 <auteur_5f_prenom>XXX </auteur_5f_prenom>
 <date_5f_action>date_action 00/00/00 </date_5f_action>
 <date_5f_redaction>date_redaction 00/00/00 </date_5f_redaction>
 <mail/>
 <texteID>TexteID </texteID>
 <texteID>1234 </texteID>
  01
</Standard>

我想创建一个xsl转换以获得此输出:

<paragraphe auteur_nom = "XXX XXX" auteur_prenom = "XXX" date_action = "00/00/00" texte_id = "205801"/>

问题是:如何在这样的格式错误的文档中创建常规XSL转换,其中标记“auteur_5f_nom”可以是:

1. <auteur_5f_nom>auteur_nom </auteur_5f_nom>
2. <auteur_5f_nom>auteur_nom XXX XXX </auteur_5f_nom>
3. <auteur_5f_nom>XXX </auteur_5f_nom>
4. <auteur_5f_nom>auteur_prenom </auteur_5f_nom>

3 个答案:

答案 0 :(得分:0)

  

我想创建一个新标记,我将其称为“p”作为上面的段落,并将我的xml源中的“标记”转换为属性,并将其文本用作属性的值。

这是你要找的吗?

<强>样式表

<?xml version="1.0" encoding="utf-8"?>

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

   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/test">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="tag">
       <p tag="{normalize-space(.)}">
           <xsl:text>some text</xsl:text>
       </p>
   </xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <p tag="tag X">some text</p>
  <p tag="X">some text</p>
  <p tag="tag">some text</p>
  <p tag="X">some text</p>
  <p tag="tag2">some text</p>
</test>

答案 1 :(得分:0)

  

我想创建一个新标签,我将其称为“p”作为段落   上面,并将我的xml源中的“标记”转换为属性和   使用其文本作为属性的值。

如果这是它的要点,可以通过以下方式简单地完成:

<xsl:template match="tag">
    <p>
        <xsl:attribute name="tag" select="."/>
    </p>
</xsl:template>

或者,如果您更喜欢较短的一个:

<xsl:template match="tag">
    <p tag="{.}"/>
</xsl:template>

答案 2 :(得分:0)

  

它无法识别“标签X”和“标签”之间的区别。

您的匹配('。','tag \ s。')函数表示找到文本“tag”,后跟单个空格,后跟字符串中任意位置的“ZERO或MORE”字符。将“”更改为“+”,您现在正在查找“一个或多个”字符,现在将匹配“标记X”,但不匹配“标记”。请注意“。”将匹配任何字符,包括更多的空格。在进行匹配比较之前,您可能希望采用其他人提供的使用normalize-space()的建议。