2012-05-01 48 views
1

我真正的新XSLT,我的大部分工作是與InDesign中,所以請與我裸:)應用XSLT添加標籤

我出口從InDesign的XML文件。該文件中的文本包含指上述特定語法概念的上標;但是,這些上標以XML文件中的文本形式導出。我需要編寫一個XSLT,以便將其應用於InDesign文件時,它只會爲上標添加一點標籤。

這是它是如何目前導出爲:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Root> 
<Content> 
<PhraseNative aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="260.5"> 
<Phrase> 1. Mark is1a playing2 videogames.</Phrase> 
</PhraseNative> 
</Content> 
</Root> 

這應該是最終代碼。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Root> 
<Content> 
<PhraseNative aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="260.5"> 
<Phrase> 1. Mark is<tag>1a</tag> playing<tag>2</tag> videogames.</Phrase> 
</PhraseNative> 
</Content> 
</Root> 

這些標籤將始終顯示每當一個數字和一個字母是一個字符串的兩個或三個最後一位數字。有時它只會是一個數字。輸出完全不會改變。這只是因爲標籤在導出回網頁時不會丟失。

任何幫助將不勝感激。

在此先感謝!

+0

你重新標記這xslt1,但有你不能使用xslt2(因此是使用XSLT 2 _much_容易) –

+0

InDesign不支持2.0然而,我當時想通了,我可以應用它通過任何理由另一個軟件。它被重新標記爲XSLT 2.0。 – babyeumbrella

+1

好吧,那麼@ DevNull的答案似乎很好:-) –

回答

1

你用XSLT 2.0標記了這個問題,所以這裏有一個2.0選項。

注意:我必須爲aid前綴添加虛擬xmlns。

此外,你很可能需要改進正則表達式,但這應該讓你開始。

XML輸入

<Root> 
    <Content> 
    <PhraseNative aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="260.5" xmlns:aid="somexmlns"> 
     <Phrase> 1. Mark is1a playing2 videogames.</Phrase> 
    </PhraseNative> 
    </Content> 
</Root> 

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:aid="somexmlns"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="Phrase"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <xsl:analyze-string select="." regex="([a-z]+)([0-9]+[a-z]*)"> 
     <xsl:matching-substring> 
      <xsl:value-of select="regex-group(1)"/> 
      <tag> 
      <xsl:value-of select="regex-group(2)"/>  
      </tag> 
     </xsl:matching-substring> 
     <xsl:non-matching-substring><xsl:value-of select="."/></xsl:non-matching-substring> 
     </xsl:analyze-string> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

XML輸出

<Root> 
    <Content> 
     <PhraseNative xmlns:aid="somexmlns" aid:table="cell" aid:crows="1" aid:ccols="1" 
        aid:ccolwidth="260.5"> 
     <Phrase> 1. Mark is<tag>1a</tag> playing<tag>2</tag> videogames.</Phrase> 
     </PhraseNative> 
    </Content> 
</Root> 

使用Saxon-HE 9.3進行測試。

+0

如果你有時間,並想向我解釋這是如何工作的,我會永遠感激。我發現這個XSLT選項非常適合與InDesign一起工作,所以如果我能理解將來我可能會做些什麼,那麼我將不必再次打擾你了:) – babyeumbrella