2011-04-07 114 views
2

如何使用XSLT,從我的輸入xml中只選擇一些xml標記到我的輸出XML中? 例如輸入:使用XSLT來削減XML輸出

<Country value="USA"> 
    <State value="KY> 
     <City value="Hebron" /> 
     <City value="Lexington" /> 
     <City value="Owensboro" /> 
     <City value="Jonesville" /> 
    </State> 
    <State value="OH"> 
     <City value="Cincinnati" /> 
     <City value="Columbus" /> 
     <City value="Cleveland" /> 
     <City value="Jonesville" /> 
    </State> 
    <State value="IN" > 
     <City value="Indianapolis" /> 
    </State> 
</Country> 

所以,守在原地的國家/國家代碼,且只能複製希伯倫和辛辛那提?

預期輸出:

<Country value="USA"> 
    <State value="KY> 
     <City value="Hebron" /> 
    </State> 
    <State value="OH"> 
     <City value="Cincinnati" /> 
    </State> 
</Country> 
+1

什麼定義了你想保留的標籤?首先在每個州? – 2011-04-07 18:11:34

+0

既然你有''標籤,這意味着你會有其他國家,其中''可能不適合(省,縣等)。您需要完全指定問題。 – 2011-04-07 18:13:02

+0

可以有超過1個國家,但對於這個例子(不是真正的標籤),國家將足以爲其他國家的省份... – 2011-04-07 18:26:04

回答

2

以下樣式:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="City[not(@value='Hebron' or @value='Cincinnati')]" /> 
</xsl:stylesheet> 

在此輸入:

<Country value="USA"> 
    <State value="KY"> 
     <City value="Hebron" /> 
     <City value="Lexington" /> 
     <City value="Owensboro" /> 
    </State> 
    <State value="OH"> 
     <City value="Cincinnati" /> 
     <City value="Columbus" /> 
     <City value="Cleveland" /> 
    </State> 
</Country> 

產生以下結果:

<Country value="USA"> 
    <State value="KY"> 
     <City value="Hebron" /> 
    </State> 
    <State value="OH"> 
     <City value="Cincinnati" /> 
    </State> 
</Country> 

這個樣式表使用identity transform所有複製,但在產量不變的不需要的節點。

又如

您可能還需要刪除不具有期望城市的任何State元素。這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="City[not(@value='Hebron' or @value='Cincinnati')]"/> 
    <xsl:template 
      match="State[not(City[@value='Hebron' or @value='Cincinnati'])]"/> 
</xsl:stylesheet> 

應用於此輸入:

<Country value="USA"> 
    <State value="KY"> 
     <City value="Hebron" /> 
     <City value="Lexington" /> 
     <City value="Owensboro" /> 
    </State> 
    <State value="OH"> 
     <City value="Cincinnati" /> 
     <City value="Columbus" /> 
     <City value="Cleveland" /> 
    </State> 
    <State value="MO"> 
     <City value="St. Louis" /> 
    </State> 
</Country> 

產地:

<Country value="USA"> 
    <State value="KY"> 
     <City value="Hebron" /> 
    </State> 
    <State value="OH"> 
     <City value="Cincinnati" /> 
    </State> 
</Country> 
1

將只留下特定城市:

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


<xsl:template match="City[@value != 'Hebron' and @value != 'Cincinnati']"/> 

將只留下第一個城市:

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


<xsl:template match="City[position() &gt; 1]"/> 
1

這裏是我的(可能不夠)2.0解決方案。城市是作爲參數傳遞的正則表達式。

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

    <xsl:param name="Cities" select="'Cincinnati|Hebron'"/> 

    <xsl:template match="State"> 
    <xsl:if test="exists(City[matches(@value, $Cities)])"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:if> 
    </xsl:template> 

    <xsl:template match="State/City"> 
    <xsl:if test="matches(@value, $Cities)"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:if> 
    </xsl:template> 

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

</xsl:stylesheet> 
+0

@stand:參數 – 2011-04-07 19:06:37

+0

@Ajjandro:是的,我認爲一個正則表達式可能會提供更大的靈活性,所以你可以選擇「以C開頭的城市」或類似的東西,當然你必須是對於正則表達式來說這是另一個問題。 – stand 2011-04-07 19:57:54

+0

@stand:但更重要的是,您不得打開樣式表失敗的窗口,從而允許可能包含禁用RegExp的參數與空序列相匹配。 – 2011-04-07 20:06:01