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

问题描述 投票:0回答:1

我正在尝试使用XSLT在不同的节点和值之间输出重复的值。我希望节点元素是动态的,以便它可以在名称空间前缀之后跟踪不同的值,例如:car:ID,car:Name,car:Location_name或更多。我知道我可以使用函数Local-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>

使用的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<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"
    version="3.0">

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:value-of select="//car:ID[ let $v:=string(.),$t:=@car:type return not( preceding::car:ID[string(.) = $v and @car:type=$t]) ]/(let $v:=string(.), $t:=@car:type,$c:=1+count(following::car:ID[string(.)=$v and $t=@car:type]) ,$c:=1+count(following::car:*[string(.)=$v]) return if ($c > 1) then concat( string(.), ' occurs ', $c, ' times for type ', $t, '&#13;&#10;') else () )"/>
    </xsl:template>
</xsl:stylesheet>

xslt显示的输出:

Car_001 occurs 2 times for type 
 Automatic occurs 2 times for type Transmission_Reference_Type

但是我希望它显示

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
xml xslt nokogiri oxygenxml
1个回答
0
投票

如果您正在寻找XSLT解决方案(而不是单行XPath表达式),则可以使用xsl:for-each-group和复合键:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.