2013-05-22 62 views
0

我對XSLT非常陌生,我不是程序員,所以對於我可能愚蠢的問題感到抱歉。用正則表達式查找包含節點的特定文本

我需要找到某些引用可以是這樣的:

BSK StPO-`<emphasis role="smallcaps">Burger,</emphasis>` Art. 4 N 5 

其中包含引用的文本節點可以是不同的父元素,例如內parafootnote

比我想要將整個引用包含在refid元素中,並使用引用的部分作爲id。

<refid multi-idref="K_BSK_STPO-JSTPO_StPO_Art4_5"> 
    BSK STGB I-`<span class="smallcaps">Burger,</span>` Art. 4 N 5 
</refid>` 

問題是emphasis元素:我找不到一個方法「圍繞」它。我發現this answer有類似的問題,我試圖將其應用於我的問題,但我沒有成功。這個腳本部分沒有找到任何引用。

這是我的代碼的一部分。 $DokumentName指的是全局定義的參數。羅馬數字在引文中的部分是可選的:

<xsl:template match="text()[matches(., 'BSK\s+(\p{L}{2,5})\s+(I|II|III|IV|V|VI|VII)?\p{P}')]"> 
    <xsl:variable name="vCur" select="."/> 
    <xsl:variable name="pContent" select="string(.)"/> 
    <xsl:analyze-string select="$pContent" regex="BSK\s+(\p{{L}}{{2,5}})\s+(I|II|III|IV|V|VI|VII)?\p{{P}}" flags="i"> 
    <xsl:matching-substring> 
     <xsl:variable name="figureToTargetId"> 
     <xsl:choose> 
      <xsl:when test="matches(., 'BSK\s+(\p{L}{2,5})\s+(I|II|III|IV|V|VI|VII)?\p{P}')">     
      <xsl:analyze-string select="." regex="(\p{{L}}{{2,5}})\s+(I|II|III|IV|V|VI|VII)"> 
       <xsl:matching-substring> 
       <xsl:value-of select="concat($DokumentName, '_', regex-group(1), regex-group(2))"/> 
       </xsl:matching-substring> 
      </xsl:analyze-string> 
      </xsl:when> 
      <xsl:otherwise> 
      <xsl:analyze-string select="." regex="(\p{{L}}{{2,5}})"> 
       <xsl:matching-substring> 
       <xsl:value-of select="concat($DokumentName, '_', regex-group(1))"/> 
       </xsl:matching-substring> 
      </xsl:analyze-string> 
      </xsl:otherwise> 
     </xsl:choose> 
     </xsl:variable> 
     <xsl:variable name="figureFromTargetId"> 
     <xsl:if test="matches($vCur, 'BSK\s+(\p{L}{2,5})\s+(I|II|III|IV|V|VI|VII)?\p{P}')"> 
      <xsl:analyze-string select="string($vCur/following-sibling::emphasis[1]/following-sibling::*[1])" regex=",?Art\.\s+(d+)\s+N\s+(d+)"> 
      <xsl:matching-substring> 
       <xsl:value-of 
       select="concat('_Art', regex-group(1), '_', regex-group(2))"/> 
      </xsl:matching-substring> 
      </xsl:analyze-string> 
     </xsl:if> 
     </xsl:variable> 
     <xsl:element name="ref-multi-id"> 
     <xsl:attribute name="multi-idref"> 
      <xsl:value-of select="concat($figureToTargetId, $figureToTargetId)"/> 
     </xsl:attribute> 
     <xsl:value-of select="."/> 
     <xsl:if test="matches($vCur, 'BSK\s+(\p{L}{2,5})\s+(I|II|III|IV|V|VI|VII)?\p{P}')"> 
      <xsl:apply-templates select="$vCur/following-sibling::emphasis[1]" mode="copy-style"/> 
      <xsl:value-of select="$vCur/following-sibling::emphasis[1]/following-sibling::*[1][matches(.,',?Art\.\s+(d+)\s+N\s+(d+)')]"/> 
     </xsl:if> 
     </xsl:element> 
    </xsl:matching-substring> 
    <xsl:non-matching-substring> 
     <xsl:value-of select="."/> 
    </xsl:non-matching-substring> 
    </xsl:analyze-string> 
</xsl:template> 

<xsl:template match="emphasis[@role='smallcaps']" mode="copy-style"> 
    <xsl:element name="span"> 
    <xsl:attribute name="class"> 
     <xsl:value-of select="@role"/> 
    </xsl:attribute> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

任何幫助將非常感激!

回答

0

這是最後的工作代碼,我只是忘了添加的「模式」 -attribute,我不得不考慮一些更引用和替代我必須擺脫冗餘節點和節點部分。

<xsl:template match="text()[matches(., 'BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P}')]" mode="copy-style"> 
    <xsl:variable name="vCur" select="."/> 
    <xsl:variable name="pContent" select="string(.)"/> 
    <xsl:analyze-string select="$pContent" regex="BSK\s+(\p{{L}}{{2,5}})(\s+(I|II|III|IV|V|VI|VII))?\p{{P}}" flags="i"> 
     <xsl:matching-substring> 
      <xsl:variable name="figureToTargetId"> 
      <xsl:choose> 
       <xsl:when test="matches(., '(BSK)\s+(\p{L}{2,5})\s+(I|II|III|IV|V|VI|VII)\p{P}')">     
        <xsl:analyze-string select="." regex="(\p{{L}}{{2,5}})\s+(I|II|III|IV|V|VI|VII)"> 
        <xsl:matching-substring> 
         <xsl:value-of select="concat('K_BSK_', regex-group(1), regex-group(2), '_', regex-group(1))"/> 
        </xsl:matching-substring> 
        </xsl:analyze-string> 
       </xsl:when> 
       <xsl:when test="matches(., '(BSK)\s+(StPO)\p{P}') "> 
        <xsl:analyze-string select="." regex="(BSK)\s+(\p{{L}}{{2,5}})"> 
         <xsl:matching-substring> 
          <xsl:value-of select="concat('K_BSK_STPO-JSTPO_', regex-group(2))"/> 
         </xsl:matching-substring> 
        </xsl:analyze-string> 
       </xsl:when> 
       <xsl:when test="matches(., '(BSK)\s+(JStPO)\p{P}') "> 
        <xsl:analyze-string select="." regex="(BSK)\s+(\p{{L}}{{2,5}})"> 
         <xsl:matching-substring> 
          <xsl:value-of select="concat('K_BSK_STPO-JSTPO_', regex-group(2))"/> 
         </xsl:matching-substring> 
        </xsl:analyze-string> 
       </xsl:when> 
       <xsl:when test="matches(., 'BSK\s+(\p{L}{2,5})\p{P}') "> 
        <xsl:analyze-string select="." regex="BSK\s+(\p{{L}}{{2,5}})"> 
         <xsl:matching-substring> 
          <xsl:value-of select="concat('K_BSK_', regex-group(1), '_', regex-group(1))"/> 
         </xsl:matching-substring> 
        </xsl:analyze-string> 
       </xsl:when> 
      </xsl:choose> 
      </xsl:variable> 
      <xsl:variable name="figureFromTargetId"> 
       <xsl:if test="matches($vCur, 'BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P}')"> 
        <xsl:analyze-string select="string($vCur/following-sibling::emphasis[1]/following-sibling::text()[1])" regex="^,?(\s+Vor)?\s+Art\.(\s+|\p{{Zs}})(\p{{N}}{{1,4}})\s+N(\s+|\p{{Zs}})(\p{{N}}{{1,4}})"> 
        <xsl:matching-substring> 
         <xsl:choose> 
          <xsl:when test="contains(., 'Vor')"> 
           <xsl:value-of select="concat('_VorArt', regex-group(3), '_', regex-group(5))"/> 
          </xsl:when> 
          <xsl:otherwise> 
           <xsl:value-of select="concat('_Art', regex-group(3), '_', regex-group(5))"/> 
          </xsl:otherwise> 
         </xsl:choose>   
        </xsl:matching-substring>      
       </xsl:analyze-string> 
       </xsl:if> 
      </xsl:variable> 
      <xsl:element name="ref-multi-id"> 
       <xsl:attribute name="multi-idref"> 
        <xsl:value-of select="concat($figureToTargetId, $figureFromTargetId)"/> 
       </xsl:attribute> 
       <xsl:value-of select="."/> 
       <xsl:if test="matches($vCur, 'BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P}')"> 
        <xsl:apply-templates select="$vCur/following-sibling::emphasis[1]" mode="match"/>      
       </xsl:if> 
       <xsl:analyze-string select="string($vCur/following-sibling::emphasis[1]/following-sibling::text()[1])" regex="(^,?(\s+Vor)?\s+Art\.(\s+|\p{{Zs}})(\p{{N}}{{1,4}})\s+N(\s+|\p{{Zs}})(\p{{N}}{{1,4}}))"> 
        <xsl:matching-substring> 
         <xsl:value-of select="regex-group(1)"/> 
        </xsl:matching-substring>       
       </xsl:analyze-string> 
      </xsl:element> 

     </xsl:matching-substring> 
     <xsl:non-matching-substring> 
      <xsl:copy-of select="."/> 
     </xsl:non-matching-substring> 
    </xsl:analyze-string>   
</xsl:template> 
    <xsl:template match="emphasis[@role='smallcaps'][not(preceding-sibling::node()[1][self::text() and matches(., 'BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P}')])]" mode="copy-style"> 
    <xsl:element name="span"> 
      <xsl:attribute name="class"> 
       <xsl:value-of select="@role"/> 
      </xsl:attribute> 
      <xsl:apply-templates/> 
     </xsl:element>  
    </xsl:template> 
    <xsl:template match="emphasis[@role='smallcaps'][preceding-sibling::node()[1][self::text() and matches(., 'BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P}')]]" mode="match"> 
    <xsl:element name="span"> 
     <xsl:attribute name="class"> 
      <xsl:value-of select="@role"/> 
     </xsl:attribute> 
     <xsl:apply-templates/> 
     </xsl:element>  
    </xsl:template> 
    <xsl:template mode="copy-style" match="text()[matches(., '^,?(\s+Vor)?\s+Art\.(\s+|\p{Zs})(\p{N}{1,4})\s+N(\s+|\p{Zs})(\p{N}{1,4})') and preceding-sibling::emphasis[@role='smallcaps'][1] and matches(preceding-sibling::emphasis[1]/preceding-sibling::text()[1], 'BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P}')]"> 
    <xsl:variable name="pContent" select="string(.)"/> 
    <xsl:analyze-string select="$pContent" regex="^,?(\s+Vor)?\s+Art\.(\s+|\p{{Zs}})(\p{{N}}{{1,4}})\s+N(\s+|\p{{Zs}})(\p{{N}}{{1,4}})"> 
     <xsl:matching-substring/> 
     <xsl:non-matching-substring> 
      <xsl:copy-of select="."/> 
     </xsl:non-matching-substring> 
     </xsl:analyze-string> 
    </xsl:template> 
0

您的正則表達式與您顯示的字符串不匹配,因爲您需要有連字符的空格。它看起來像

BSK\s+(\p{L}{2,5})\s+(I|II|III|IV|V|VI|VII)?\p{P} 

應該

BSK\s+(\p{L}{2,5})(\s+(I|II|III|IV|V|VI|VII))?\p{P} 
相關問題