Ruby XSLT如何基于不同的元素节点和属性显示/输出重复值

Ruby XSLT如何基于不同的元素节点和属性显示/输出重复值,ruby,xml,xslt,nokogiri,oxygenxml,Ruby,Xml,Xslt,Nokogiri,Oxygenxml,我试图使用XSLT跨不同的节点和值输出重复的值。我希望节点元素是动态的,以便它可以跟踪命名空间前缀后的不同值,例如:car:ID、car:Name、car:Location\u Name或更多。我知道我可以使用函数本地名。但我不确定如何应用于我的XSLT逻辑。请帮忙 示例XML如下所示: <car:root xmlns:car="com.sample"> <Car_Input_Request> <car:Car_Details> <

我试图使用XSLT跨不同的节点和值输出重复的值。我希望节点元素是动态的,以便它可以跟踪命名空间前缀后的不同值,例如:car:ID、car:Name、car:Location\u Name或更多。我知道我可以使用函数本地名。但我不确定如何应用于我的XSLT逻辑。请帮忙 示例XML如下所示:

<car:root xmlns:car="com.sample">
<Car_Input_Request>
    <car:Car_Details>
        <car:ID>Car_001</car:ID>
        <car:Name>Fastmobile</car:Name>
        <car:Local_Name>New York</car:Local_Name>
        <car:Transmission_Reference_Type>
            <car:ID car:type="Transmission_Reference_Type">Automatic</car:ID>
        </car:Transmission_Reference_Type>
    </car:Car_Details>      
</Car_Input_Request>
    <Car_Input_Request>
        <car:Car_Details>
            <car:ID>Car_002</car:ID>
            <car:Name>Slowmobile</car:Name>
            <car:Local_Name>New York</car:Local_Name>
            <car:Transmission_Reference_Type>
                <car:ID car:type="Transmission_Reference_Type">Manual</car:ID>
            </car:Transmission_Reference_Type>
        </car:Car_Details>      
    </Car_Input_Request>
    <Car_Input_Request>
        <car:Car_Details>
            <car:ID>Car_001</car:ID>
            <car:Name>Fastmobile</car:Name>
            <car:Local_Name>New York</car:Local_Name>
            <car:Transmission_Reference_Type>
                <car:ID car:type="Transmission_Reference_Type">Automatic</car:ID>
            </car:Transmission_Reference_Type>
        </car:Car_Details>      
    </Car_Input_Request>
</car:root>
但我想让它表现出来

Car_001 occurs 2 times for type ID
Fastmobile occurs 2 times for type Name
Automatic occurs 2 times for type Transmission_Reference_Type
New York occurs 3 times for type Local_Name

如果您正在寻找XSLT解决方案,而不是单行XPath表达式,则可以使用xsl:for每个具有复合键的组:

<xsl:stylesheet
    xmlns:car="com.sample"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    expand-text="yes"
    version="3.0">

    <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:for-each-group select="//car:Car_Details/*" group-by="local-name(), normalize-space()" composite="yes">
        <xsl:if test="current-group()[2]">
          <xsl:text>{normalize-space()} occurs {count(current-group())} times for {local-name()}&#10;</xsl:text>
        </xsl:if>
      </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

第一行//car:ID[让$v:=string.,$t:=@car:type返回非前置::car:ID[字符串=$v和@car:type=$t]]查找所有没有dup值的car:ID元素。然后/让$v:=string.,$t:=@car:type,$c:=1+countfollowing::car:ID[string.=$v和$t=@car:type],$c:=1+countfollowing::car:[string.=$v]返回如果$c>1,则返回concat字符串。,'accurs',$c,'times for type',$t',&13&10;' 如果count大于1,则显示重复值,并输出变量值,如$t、$c等。对于查找序列中所有重复值的单个XPath表达式,请参阅:Thank Tim,这非常有用!另一个问题,你知道我如何使每组选择动态?如果我们有一个新的API请求是car:Seller\U详细信息怎么办?或汽车:买家详细信息。。。例如,我们可以选择select=car:[..]?您可以选择select=//car:[count祖先::car:*=1]/*以避免选择car:root或car:car\u Details的子项。
Car_001 occurs 2 times for type ID
Fastmobile occurs 2 times for type Name
Automatic occurs 2 times for type Transmission_Reference_Type
New York occurs 3 times for type Local_Name
<xsl:stylesheet
    xmlns:car="com.sample"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    expand-text="yes"
    version="3.0">

    <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:for-each-group select="//car:Car_Details/*" group-by="local-name(), normalize-space()" composite="yes">
        <xsl:if test="current-group()[2]">
          <xsl:text>{normalize-space()} occurs {count(current-group())} times for {local-name()}&#10;</xsl:text>
        </xsl:if>
      </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>