2010-06-30 70 views
0

我仍然是XSLT的初學者,但我手頭有一項艱鉅的任務。複雜的XSL轉換

我有一個非xml文件,需要進行轉換。該文件的格式A S如下:

type1 
type1line1 
type1line2 
type1line3 
type2 
type2line1 
type2line2 
type3 
type3line1 
type3line2 

類型(type1, type2, ...)使用不具有特定的順序某些代碼指定。下面的每種類型都有多個line

所以,我需要轉換這個文件,但問題是,對於每個type我必須爲它的每個底層線做不同的轉換。

現在,我可以逐行讀取字符串,並確定一個新類型已經開始,但我不知道如何設置一個標誌(指示類型)在底層行中使用它。

這是我現在所擁有的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> 
    <xsl:param name="testString" as="xs:string"> 
    type1 
    line1 
    line2 
    type1 
    line1 
    </xsl:param> 
    <xsl:template match="/"> 
    <xsl:call-template name="main"> 
     <xsl:with-param name="testString" select="$testString"/> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="main"> 
    <xsl:param name="testString"/> 
    <xsl:variable name="iniFile" select="$testString"/> 
    <config> 
     <xsl:analyze-string select="$iniFile" regex="\n"> 
     <xsl:non-matching-substring> 
      <item> 
      <xsl:choose> 
       <xsl:when test="starts-with(., 'type1')"> 
    <!-- do a specific transformation-->  
       </xsl:when> 
       <xsl:when test="starts-with(., 'type2')"> 
    <!-- do another transformation-->  
       </xsl:when> 
      </xsl:choose> 
      </item> 
     </xsl:non-matching-substring> 
     </xsl:analyze-string> 
    </config> 
    </xsl:template> 
</xsl:stylesheet> 

關於如何解決這個問題的任何想法。

+1

你一定要明白XSLT是爲了轉換** XML文件**? ;) – 2010-06-30 13:13:50

+1

將此問題標記爲XSLT2以避免像Niels'這樣的評論。 @Niels van der Rest:XSLT2可以處理未解析的文檔。 – 2010-06-30 13:28:57

+0

@Alejandro感謝您糾正我,我沒有意識到這一點:) – 2010-06-30 13:34:35

回答

0

我認爲XSLT 2.1將允許您使用像for-each-group這樣的強大的東西來處理像字符串這樣的原子值序列,但是對於XSLT 2.0,只有節點序列才具有如此強大的功能,所以我第一步使用XSLT 2.0使用純數據字符串我想處理/組是創建元素。因此,您可以對數據進行標記,將每個標記包裝到某個元素中,然後使用for-each-group group-starting-with來處理每個組,並以'^ type [0-9] + $'之類的模式開始。 你還沒有真正告訴我們你想用什麼樣的數據,一旦你已經確定了一個組,以便採取以下作爲例如,你可以適應:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

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

    <xsl:param name="input" as="xs:string">type1 
type1line1 
type1line2 
type1line3 
type2 
type2line1 
type2line2 
type3 
type3line1 
type3line2</xsl:param> 

    <xsl:template name="main"> 
    <xsl:variable name="lines" as="element(item)*"> 
     <xsl:for-each select="tokenize($input, '\n')"> 
     <item><xsl:value-of select="."/></item> 
     </xsl:for-each> 
    </xsl:variable> 
    <xsl:for-each-group select="$lines" group-starting-with="item[matches(., '^type[0-9]+$')]"> 
     <xsl:choose> 
     <xsl:when test=". = 'type1'"> 
      <xsl:apply-templates select="current-group() except ." mode="m1"/> 
     </xsl:when> 
     <xsl:when test=". = 'type2'"> 
      <xsl:apply-templates select="current-group() except ." mode="m2"/> 
     </xsl:when> 
     <xsl:when test=". = 'type3'"> 
      <xsl:apply-templates select="current-group() except ." mode="m3"/> 
     </xsl:when> 
     </xsl:choose> 
    </xsl:for-each-group> 
    </xsl:template> 

    <xsl:template match="item" mode="m1"> 
    <foo> 
     <xsl:value-of select="."/> 
    </foo> 
    </xsl:template> 

    <xsl:template match="item" mode="m2"> 
    <bar> 
     <xsl:value-of select="."/> 
    </bar> 
    </xsl:template> 

    <xsl:template match="item" mode="m3"> 
    <baz> 
     <xsl:value-of select="."/> 
    </baz> 
    </xsl:template> 

</xsl:stylesheet> 

當撒克遜9(命令行選項適用-IT :主要-xsl:sheet.xsl)的結果是

<foo>type1line1</foo> 
<foo>type1line2</foo> 
<foo>type1line3</foo> 
<bar>type2line1</bar> 
<bar>type2line2</bar> 
<baz>type3line1</baz> 
<baz>type3line2</baz>