2014-02-11 59 views
0

我正在嘗試創建一個xsl文件,它將通過一個xml文件並僅處理以特定字符集開頭的標記。我應該提到,我對xsl文件相當陌生,所以這可能很容易完成,但我只是不知道如何。XSL如何選擇以特定字符集開頭的每個選項卡?

我的XML文件看起來與此類似:

<generic_etd> 
    <associated_tags> 
     <master> 
     <dc.contributor>contributor</dc.contributor> 
     </master> 
     <related> 
     <dc.contributor.role>contributor role</dc.contributor.role> 
     </related> 
    </associated_tags> 
    <associated_tags> 
     <master> 
     <dc.contributor>sponsor</dc.contributor> 
     </master> 
     <related> 
     <dc.contributor.role>sponsor role</dc.contributor.role> 
     </related> 
    </associated_tags> 
    <dc.creator>gradstudent2</dc.creator> 
    <dc.date>2014-02-11</dc.date> 
    <dc.description>description</dc.description> 
    <thesis.degree.discipline>Business Administration</thesis.degree.discipline> 
<generic_etd> 

我希望我的XSL文件到開頭只有過程變量「DC。」

這是我想出迄今XSL文件:

<xsl:template match="*"> 
    <xsl:choose> 
     <!-- Process the tags we are interested in --> 
     <xsl:when test="contains(name(),'dc.')"> 
      <xsl:variable name="newtag" select="concat('dc:',substring-after(name(),'.'))"/> 
      <xsl:choose> 
       <xsl:when test="contains($newtag, '.')"> 
        <xsl:element name="{substring-before($newtag,'.')}"> 
         <xsl:apply-templates/> 
        </xsl:element> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:element name="{$newtag}"> 
         <xsl:apply-templates/> 
        </xsl:element> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:when> 
    </xsl:choose> 
</xsl:template> 

但是它產生的文件丟失dc.contributor並從輸出dc.contributor.role。其實,我想在這個例子中排除dc.contributor.role,但是我會在另一個我正在處理的文件中需要它。

我的問題是我去哪裏錯了?

謝謝。

+0

嚴正....你是否試圖將dc。如果是這樣,你也會想要在你的替代xsl:元素上聲明這一點。如果沒有,則不能在元素名稱中使用冒號;這是保留用作名稱空間前綴。 – keshlam

+0

是的,我正在命名它。我只顯示了我遇到問題的部分代碼。 – user5013

回答

0

這是一個樣例樣式表。它直接匹配以dc開頭的元素

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:dc="http://www.example.com"> 
    <xsl:output method='xml' indent='yes'/> 


    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:namespace name="dc">http://www.example.com</xsl:namespace> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[starts-with(name(),'dc.')]"> 
     <xsl:variable name="newtag" select="concat('dc:',substring-after(name(),'.'))"/> 
     <xsl:element name="{$newtag}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

那麼這是一個更清潔的實現,那麼我想出了。感謝這個例子。 – user5013

+0

您可能想要檢查名稱是否以「dc」開頭。而不是它是否包含它,只是爲了使它更強大......但除此之外,這應該確實有效。 – keshlam

+0

@keshlam,如果你讀了第二句話,我將與開頭一起。也許我在複製OP代碼時忘了改變它。 :) –

相關問題