XSLT for XML用于使用for-each函数

时间:2014-07-22 05:48:15

标签: xslt

我将输入XML作为

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<item>
              <UID>1000909090</UID>
    <functionalName lang="en">Filter</functionalName>
    <functionalName lang="hin">Filter1</functionalName>
    <functionalName lang="en">Filter2</functionalName>
    <functionalName lang="hin">Filter3</functionalName>
</item>
<item>
         <UID>1000909091</UID>
    <functionalName lang="en">Filter4</functionalName>
    <functionalName lang="chi">Filter5</functionalName>
    <functionalName lang="en">Filter6</functionalName>
</item>
</document>

我想循环遍历标记&#34; functionalName&#34;为每种语言捕捉价值。输出需要存储在元素&#34; VALUE_MVL&#34;对于每种特定语言。 来自第一个UID&#34; 1000909090&#34;的输出XML的片段有语言&#34; en&#34;应该是

 <MultiValueAttribute name="VALUE_MVL">
           <ValueList>
              <Value>Filter</Value>
              <Value>Filter2</Value>
           </ValueList>
 </MultiValueAttribute>

下面提到了所需的输出XML

预期输出完整XML

 <?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
 <Body>
  <CatalogItem key="FUNCTIONALNAME_MVL-1000909090-en">
     <MasterCatalog>
        <RevisionID>
           <BaseName>FUNCTIONALNAME_MVL</BaseName>
           <Version />
           <DBID />
        </RevisionID>
     </MasterCatalog>
     <ItemData>
        <Attribute name="PRODUCTID">
           <Value>1000909090</Value>
        </Attribute>
        <Attribute name="PRODUCTIDEXT">
           <Value>en</Value>
        </Attribute>
        <MultiValueAttribute name="VALUE_MVL">
           <ValueList>
              <Value>Filter</Value>
              <Value>Filter2</Value>
           </ValueList>
        </MultiValueAttribute>
     </ItemData>
     <RelationshipData>
        <Relationship>
           <RelationType>FUNCTIONALNAME_MVL_Item_Master</RelationType>
           <RelatedItems count="1">
              <RelatedItem referenceKey="1000909090" />
           </RelatedItems>
        </Relationship>
     </RelationshipData>
  </CatalogItem>
  <CatalogItem key="FUNCTIONALNAME_MVL-1000909090-hin">
     <MasterCatalog>
        <RevisionID>
           <BaseName>FUNCTIONALNAME_MVL</BaseName>
           <Version />
           <DBID />
        </RevisionID>
     </MasterCatalog>
     <ItemData>
        <Attribute name="PRODUCTID">
           <Value>1000909090</Value>
        </Attribute>
        <Attribute name="PRODUCTIDEXT">
           <Value>hin</Value>
        </Attribute>
        <MultiValueAttribute name="VALUE_MVL">
           <ValueList>
              <Value>Filter1</Value>
              <Value>Filter3</Value>
           </ValueList>
        </MultiValueAttribute>
     </ItemData>
     <RelationshipData>
        <Relationship>
           <RelationType>FUNCTIONALNAME_MVL_Item_Master</RelationType>
           <RelatedItems count="1">
              <RelatedItem referenceKey="1000909090" />
           </RelatedItems>
        </Relationship>
     </RelationshipData>
  </CatalogItem>
   </Body>
   <Body>
   <CatalogItem key="FUNCTIONALNAME_MVL-1000909091-en">
     <MasterCatalog>
        <RevisionID>
           <BaseName>FUNCTIONALNAME_MVL</BaseName>
           <Version />
           <DBID />
        </RevisionID>
     </MasterCatalog>
     <ItemData>
        <Attribute name="PRODUCTID">
           <Value>1000909091</Value>
        </Attribute>
        <Attribute name="PRODUCTIDEXT">
           <Value>en</Value>
        </Attribute>
        <MultiValueAttribute name="VALUE_MVL">
           <ValueList>
              <Value>Filter4</Value>
              <Value>Filter6</Value>
           </ValueList>
        </MultiValueAttribute>
     </ItemData>
     <RelationshipData>
        <Relationship>
           <RelationType>FUNCTIONALNAME_MVL_Item_Master</RelationType>
           <RelatedItems count="1">
              <RelatedItem referenceKey="1000909091" />
           </RelatedItems>
        </Relationship>
     </RelationshipData>
   </CatalogItem>
   <CatalogItem key="FUNCTIONALNAME_MVL-1000909091-chi">
     <MasterCatalog>
        <RevisionID>
           <BaseName>FUNCTIONALNAME_MVL</BaseName>
           <Version />
           <DBID />
        </RevisionID>
     </MasterCatalog>
     <ItemData>
        <Attribute name="PRODUCTID">
           <Value>1000909091</Value>
        </Attribute>
        <Attribute name="PRODUCTIDEXT">
           <Value>chi</Value>
        </Attribute>
        <MultiValueAttribute name="VALUE_MVL">
           <ValueList>
              <Value>Filter5</Value>
           </ValueList>
        </MultiValueAttribute>
     </ItemData>
     <RelationshipData>
        <Relationship>
           <RelationType>FUNCTIONALNAME_MVL_Item_Master</RelationType>
           <RelatedItems count="1">
              <RelatedItem referenceKey="1000909091" />
           </RelatedItems>
        </Relationship>
     </RelationshipData>
  </CatalogItem>
 </Body>
 </CatalogItem>

我使用的XSLT,但它不适用于VALUE_MVL标记。请帮助

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

<xsl:output indent="yes"/>

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

   <xsl:template match="document"> 
<CatalogItem>
<xsl:for-each select="item">
<Body>    
<xsl:for-each select="functionalName[generate-id() = generate-id(key('functional', concat  (generate-id(..), '|', @lang))[1])]"> 
<CatalogItem>
<xsl:attribute name="key">
<xsl:value-of select="concat('FUNCTIONALNAME_MVL','-',ancestor::item/UID,'-',@lang)"/>                                                  
</xsl:attribute>
<MasterCatalog>
    <RevisionID>
        <BaseName>FUNCTIONALNAME_MVL</BaseName>
        <Version/>
        <DBID/>
    </RevisionID>
</MasterCatalog>
<ItemData>
    <Attribute name="PRODUCTID">
        <Value>
            <xsl:value-of select="ancestor::item/UID"/>
        </Value>
    </Attribute>
    <Attribute name="PRODUCTIDEXT">
        <Value>
            <xsl:value-of select="@lang"/>
        </Value>
    </Attribute>                                                
    <MultiValueAttribute>
        <xsl:attribute name="name">VALUE_MVL</xsl:attribute>
        <ValueList>         
            <xsl:for-each select=".">                                               
                <Value>
        <xsl:value-of select="../functionalName"/> 
                </Value>
            </xsl:for-each>
        </ValueList>
    </MultiValueAttribute>
</ItemData>
<RelationshipData>
    <Relationship>
        <RelationType>FUNCTIONALNAME_MVL_Item_Master</RelationType>
        <RelatedItems>
            <xsl:attribute name="count">1</xsl:attribute>
            <RelatedItem>
                <xsl:attribute name="referenceKey">
            <xsl:value-of select="ancestor::item/UID"/>
                </xsl:attribute>
            </RelatedItem>
        </RelatedItems>
    </Relationship>
</RelationshipData>
 </CatalogItem>
 </xsl:for-each>
 </Body>          
 </xsl:for-each>
 </CatalogItem>
 </xsl:template> 
 </xsl:stylesheet>

2 个答案:

答案 0 :(得分:0)

我已在共享XSLT中添加了变量(UID和Lang),并使用它们来过滤所需的'functionalName'结果。请查看并查看输出:

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

  <xsl:output indent="yes"/>

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

  <xsl:template match="document"> 
    <CatalogItem>
      <xsl:for-each select="item">
        <Body>    
          <xsl:for-each select="functionalName[generate-id() = generate-id(key('functional', concat  (generate-id(..), '|', @lang))[1])]"> 
            <xsl:variable name="UID" select="ancestor::item/UID"/>
            <xsl:variable name="Lang" select="@lang"/>
            <CatalogItem>
              <xsl:attribute name="key">
                <xsl:value-of select="concat('FUNCTIONALNAME_MVL','-',ancestor::item/UID,'-',@lang)"/>                                                  
              </xsl:attribute>
              <MasterCatalog>
                <RevisionID>
                  <BaseName>FUNCTIONALNAME_MVL</BaseName>
                  <Version/>
                  <DBID/>
                </RevisionID>
              </MasterCatalog>
              <ItemData>
                <Attribute name="PRODUCTID">
                  <Value>
                    <xsl:value-of select="ancestor::item/UID"/>
                  </Value>
                </Attribute>
                <Attribute name="PRODUCTIDEXT">
                  <Value>
                    <xsl:value-of select="@lang"/>
                  </Value>
                </Attribute>                                                
                <MultiValueAttribute>
                  <xsl:attribute name="name">VALUE_MVL</xsl:attribute>
                  <ValueList>
                    <xsl:message><xsl:value-of select="$Lang"/></xsl:message>
                    <xsl:message><xsl:value-of select="$UID"/></xsl:message>
                    <xsl:for-each select="parent::item/functionalName[parent::item/UID=$UID and @lang=$Lang]">                                               
                      <Value>
                        <xsl:value-of select="text()"/> 
                      </Value>
                    </xsl:for-each>
                  </ValueList>
                </MultiValueAttribute>
              </ItemData>
              <RelationshipData>
                <Relationship>
                  <RelationType>FUNCTIONALNAME_MVL_Item_Master</RelationType>
                  <RelatedItems>
                    <xsl:attribute name="count">1</xsl:attribute>
                    <RelatedItem>
                      <xsl:attribute name="referenceKey">
                        <xsl:value-of select="ancestor::item/UID"/>
                      </xsl:attribute>
                    </RelatedItem>
                  </RelatedItems>
                </Relationship>
              </RelationshipData>
            </CatalogItem>
          </xsl:for-each>
        </Body>          
      </xsl:for-each>
    </CatalogItem>
  </xsl:template> 
</xsl:stylesheet>

答案 1 :(得分:0)

尝试替换

<xsl:for-each select=".">
  <Value>
    <xsl:value-of select="../functionalName"/>
  </Value>
</xsl:for-each>

<xsl:for-each select="../functionalName[@lang=current()/@lang]">
  <Value>
    <xsl:value-of select="."/>
  </Value>
</xsl:for-each>