XSLT:如何在密钥匹配中表示OR

时间:2014-08-01 07:04:53

标签: xslt

我将输入XML作为

<document>
<item>
<gtin>4341</gtin>
    <functionalName lang="en">Filte</functionalName>
    <functionalName lang="en">test1</functionalName>
    <functionalName lang="chi">Filters2</functionalName>
    <functionalName lang="hin">Filters3</functionalName>
    <gtinName lang="en">gtinName1</gtinName>
    <gtinName lang="en">gtinName2</gtinName>
    <gtinName lang="hin">gtinName3</gtinName>
</item>
<item>
<gtin>4342</gtin>
    <functionalName lang="en">Filte</functionalName>
    <functionalName lang="chi">Filters</functionalName>
    <functionalName lang="en">Filters1</functionalName>
    <gtinName lang="en">gtinName1</gtinName>
    <gtinName lang="chi">gtinName2</gtinName>
    <gtinName lang="chi">gtinName3</gtinName>
</item>
</document>

我希望循环使用每种语言并根据语言

获取关于组的计数

预期输出XML应为

<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
  <Relationship>
     <RelationType>Descriptions_for_Item</RelationType>
     <RelatedItems count="3">
        <RelatedItem1 referenceKey="ITEM_DESCRIPTION-4341-en" />
        <RelatedItem1 referenceKey="ITEM_DESCRIPTION-4341-chi" />
        <RelatedItem1 referenceKey="ITEM_DESCRIPTION-4341-hin" />
     </RelatedItems>
  </Relationship>
</RelationshipData>
<RelationshipData>
  <Relationship>
     <RelationType>Descriptions_for_Item</RelationType>
     <RelatedItems count="2">
        <RelatedItem1 referenceKey="ITEM_DESCRIPTION-4342-en" />
        <RelatedItem1 referenceKey="ITEM_DESCRIPTION-4342-chi" />
     </RelatedItems>
  </Relationship>
</RelationshipData>
</CatalogItem>

我使用的XSLT示例,但它没有给我预期的输出

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

<xsl:output indent="yes"/>

<xsl:key name="functional" match="gtinName" use="concat(generate-id(..), '|', @lang)" />  

<xsl:template match="document"> 
<CatalogItem>
    <xsl:for-each select="item">
        <RelationshipData>
            <Relationship>
                <RelationType>Descriptions_for_Item</RelationType>
                   <RelatedItems count="{count(gtinName[generate-id() = generate-id(key('functional', concat(generate-id(..), '|', @lang))[1])])}">
                      <xsl:apply-templates select="gtinName[generate-id() = generate-id(key('functional', concat(generate-id(..), '|', @lang))[1])]"/> 
                   </RelatedItems>
            </Relationship>
        </RelationshipData>
    </xsl:for-each>
</CatalogItem>
</xsl:template> 

 <xsl:template match="gtinName|functionalName">        
 <RelatedItem1 referenceKey="{concat('ITEM_DESCRIPTION','-',ancestor::item/gtin,'-    ',@lang)}"/>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

如果您想要“functionName”或“gtimName”上的语言属性计数,请将您的密钥更改为

<xsl:key name="functional" match="gtinName|functionalName" use="concat(generate-id(..), '|', @lang)" />  

然后,要获得语言的计数,请执行此操作

count((functionalName|gtinName)[generate-id() = generate-id(key('functional', concat(generate-id(..), '|', @lang))[1])])}">

试试这个XSLT

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

<xsl:output indent="yes"/>

<xsl:key name="functional" match="gtinName|functionalName" use="concat(generate-id(..), '|', @lang)" />  

<xsl:template match="document"> 
<CatalogItem>
    <xsl:for-each select="item">
        <RelationshipData>
            <Relationship>
                <RelationType>Descriptions_for_Item</RelationType>
                   <xsl:variable name="lang" select="(functionalName|gtinName)[generate-id() = generate-id(key('functional', concat(generate-id(..), '|', @lang))[1])]" />
                   <RelatedItems count="{count($lang)}">
                      <xsl:apply-templates select="$lang"/> 
                   </RelatedItems>
            </Relationship>
        </RelationshipData>
    </xsl:for-each>
</CatalogItem>
</xsl:template> 

<xsl:template match="gtinName|functionalName">        
 <RelatedItem1 referenceKey="{concat('ITEM_DESCRIPTION','-',ancestor::item/gtin,'-    ',@lang)}"/>
</xsl:template>
</xsl:stylesheet>

作为替代方案,如果只有“gtinName”和“functionalName”具有“lang”属性,则可以使用语法*[@lang]代替....

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

<xsl:output indent="yes"/>

<xsl:key name="functional" match="*[@lang]" use="concat(generate-id(..), '|', @lang)" />  

<xsl:template match="document"> 
<CatalogItem>
    <xsl:for-each select="item">
        <RelationshipData>
            <Relationship>
                <RelationType>Descriptions_for_Item</RelationType>
                   <xsl:variable name="lang" select="*[@lang][generate-id() = generate-id(key('functional', concat(generate-id(..), '|', @lang))[1])]" />
                   <RelatedItems count="{count($lang)}">
                      <xsl:apply-templates select="$lang"/> 
                   </RelatedItems>
            </Relationship>
        </RelationshipData>
    </xsl:for-each>
</CatalogItem>
</xsl:template> 

<xsl:template match="*[@lang]">        
 <RelatedItem1 referenceKey="{concat('ITEM_DESCRIPTION','-',ancestor::item/gtin,'-    ',@lang)}"/>
</xsl:template>
</xsl:stylesheet>