2011-04-07 116 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