XSLT 如何 display/output 根据不同的元素节点和属性复制值

XSLT how to display/output duplicate values based on different element node and attributes

我正在尝试使用 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

如果您正在寻找 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>