English 中文(简体)
XSLT(计)没有工作
原标题:XSLT count() is not working

I m trying to do the count where XML/Attributes/Attribute[@Type= ComplexAttr ]. If it is present then do this else do something else. However, count is always zero. Can someone tell what I m missing. Also can someone guide me on how to improve the last part of the xslt where I m using many xsl:if statements. Thanks in Advance.

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="type" match="Attribute" use="Type"/>
  <xsl:template match="/">
    <Data Schema="XML A">
      <xsl:apply-templates select="XML/Attributes/Attribute[generate-id() = generate-id(key( type , Type)[1])]">
        <xsl:sort select="Type" order="descending"/>
      </xsl:apply-templates>
      <errorCodes>
        <xsl:apply-templates select="XML/Attributes/Attribute" mode="errors"/>
      </errorCodes>
    </Data>
  </xsl:template>
  <xsl:template match="Attribute">
    <xsl:variable name="compType">
      <xsl:value-of select="count(XML/Attributes/Attribute[Type= ComplexAttr ])"/>
    </xsl:variable>  />
    <!--<xsl:value-of select="count($compType)"/>-->
    <xsl:if test="Type!= ComplexAttr ">
      <Attributes type="{Type}">
        <xsl:apply-templates select="key( type ,Type)" mode="out"/>
        <xsl:if test="Type= common ">
          <Collection id="" name="test">
            <ComplexAttr refId="0">
              <MaskValue />
              <xsl:choose>
                <xsl:when test="$compType > 0">
                     <xsl:apply-templates select="key( type , ComplexAttr )" mode="out"/>
                </xsl:when>
                <xsl:otherwise>
                  <Attr id="" name="Color" value="000"/>
                  <Attr id="" name="Size" value="0010"/>
                  <Attr id="" name="UPC" value=""/>
                  <Attr id="" name="Style#" value=""/>
                  <Attr id="" name="Exclude" value=""/>
                </xsl:otherwise>
              </xsl:choose>
            </ComplexAttr>
          </Collection>
        </xsl:if>
      </Attributes>
    </xsl:if>
  </xsl:template>
  <xsl:template match="Attribute" mode="out">
    <Attr id="{id}" name="{Name}" value="{Value}"/>
  </xsl:template>
  <xsl:template match="Attribute[Type= ComplexAttr ]" mode="out">
    <Attr id="{id}" name="{Name}" value="{Value}"/>
  </xsl:template>
  <xsl:template match="Attribute" mode="errors">
    <xsl:if test="Name[ Buyer ID  or  Coordinator ID  or  Retail  or 
             Master Pack Qty  or  Master Pack Height  or  Master Pack Length  or  Master Pack Weight  or
             Master Pack Width  or  Product Description  or  PO Cost  or  GTIN  or  Vendor Model ] and Value=  ">
    <errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Sample XML:

<?xml version="1.0" encoding="windows-1252"?>
<XML>
    <Attributes>
        <Attribute>
            <id>5</id>
            <Name>Buyer ID</Name>
            <Type>common</Type>
            <Value>Lee</Value>
        </Attribute>
        <Attribute>
            <id>331</id>
            <Name>Enviornment</Name>
            <Type>common</Type>
            <Value>Development</Value>
        </Attribute>
        <Attribute>
            <id>79</id>
            <Name>Retail</Name>
            <Type>common</Type>
            <Value></Value>
        </Attribute>
        <Attribute>
            <id>402</id>
            <Name>Gender</Name>
            <Type>category</Type>
            <Value>Men</Value>
        </Attribute>
    <Attribute>
         <id>1197</id> 
         <Name>UPC</Name> 
         <Type>ComplexAttr</Type> 
         <Value>Testing</Value> 
         <Path /> 
    </Attribute>
</Attributes>

OutPut: As you can see it is not giving me the expected output. It should be Attributes/Attribute[Type= common ]/Collection/ComplexAttr/ and I m getting the following

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
  <Attributes type="common">
    <Attr id="5" name="Buyer ID" value="Lee" />
    <Attr id="331" name="Enviornment" value="Development" />
    <Attr id="79" name="Retail" value="" />
    <Attr id="41" name="PlusShip" value="False" />
    <Collection id="" name="test">
      <ComplexAttr refId="0">
        <MaskValue />
        <Attr id="" name="Color" value="000" />
        <Attr id="" name="Size" value="0010" />
        <Attr id="" name="UPC" value="" />
        <Attr id="" name="Style#" value="" />
        <Attr id="" name="Exclude" value="" />
      </ComplexAttr>
    </Collection>
  </Attributes>
  <Attributes type="category">
    <Attr id="402" name="Gender" value="Men" />
    <Attr id="433" name="HeelHeight" value="" />
  </Attributes>
  <errorCodes>
    <errorCode>"value for Retail is missing."</errorCode>
  </errorCodes>
</Data>
最佳回答
  <xsl:template match="Attribute">     
   <xsl:variable name="compType">       
     <xsl:value-of select="count(XML/Attributes/Attribute[Type= ComplexAttr ])"/>

   </xsl:variable>

www.un.org/Depts/DGACM/index_spanish.htm 问题在这里是。

<代码>Attribute 元件没有儿童名称XML,因此,上述变量中的儿童的定本数值为0。

www.un.org/Depts/DGACM/index_spanish.htm 页: 1

<xsl:variable name="compType" 
     select="count(/XML/Attributes/Attribute[Type= ComplexAttr ])"/>

<>Explanation: 问题在于使用相对的XPath而不是绝对的表述。

问题回答

However, count is always zero. Can someone tell what I m missing.

<>Answer #1: 页: 1 数值为<代码>的 1. 复杂的:

<xsl:when test="count(//Attribute[@Type= ComplexAttr ]) > 0">

但您应计算<代码>号数。 含有儿童内容的内容,名称为<编码> 数值为<代码>的 1. 复杂的:

<xsl:when test="count(//Attribute[Type= ComplexAttr ]) > 0">

仅对你的《特别豁免法》作出这一改动,就会导致考试的通过。

Also can someone guide me on how to improve the last part of the xslt where I m using many xsl:if statements.

<><>Answer #2: 我将把每一条件结合起来:

<xsl:if test="Name[ Retail  or  Environment  or 
     Master Pack Qty  or  Master Pack Height ] and Value=  ">
    <errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
</xsl:if>

每一种情况都是相同的,因此没有必要将它们分开处理。

这一简化风格:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="type" match="Attribute" use="Type"/>
  <xsl:template match="/">
      <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="Attribute">
      <xsl:if test="Name[ Buyer ID  or  Coordinator ID  or  Retail  or 
             Master Pack Qty  or  Master Pack Height  or  Master Pack Length  or
             Master Pack Weight  or  Master Pack Width  or  Product Description  or 
             PO Cost  or  GTIN  or  Vendor Model ] and Value=  ">
          <errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
      </xsl:if>
  </xsl:template>
</xsl:stylesheet>

编制以下产出(根据您的意见):

<errorCode>"value for Retail is missing."</errorCode>

页: 1 PolyAttr 是因为xsl:key宣言。

如果没有这样的<代码>Attribute,如果你想要产生一些违约内容,则使用:

<xsl:template match="Attribute[Type= ComplexAttr ]"/>
<xsl:template match="Attribute[Type= common ]">
    <Attributes type="common">
        <xsl:apply-templates select="key( type , common )" mode="out"/>
        <Collection id="" name="test">
            <ComplexAttr refId="0">
                <MaskValue />
                <xsl:apply-templates select="key( type , ComplexAttr )"
                                     mode="out"/>
            </ComplexAttr>
        </Collection>
    </Attributes>
</xsl:template>
<xsl:template match="Attribute[Type= common ]
                              [not(key( type , ComplexAttr ))]"
              priority="1">
    <Attributes type="common">
        <xsl:apply-templates select="key( type , common )" mode="out"/>
        <Collection id="" name="test">
            <ComplexAttr refId="0">
                <MaskValue />
                    <Attr id="" name="Color" value="000"/>
                    <Attr id="" name="Size" value="0010"/>
                    <Attr id="" name="UPC" value=""/>
                    <Attr id="" name="Style#" value=""/>
                    <Attr id="" name="Exclude" value=""/>
            </ComplexAttr>
        </Collection>
    </Attributes>
</xsl:template>
<xsl:template match="Attribute">
    <Attributes type="{Type}">
        <xsl:apply-templates select="key( type ,Type)" mode="out"/>
    </Attributes>
</xsl:template>

<>说明>:@ priority 不依赖回收机制。 就象牙一样,更有拉动风格。





相关问题
SELECT command to calculate percentage

I m trying to get the percentage of each video I have in my database based on its view count against all other videos. I m then trying to display all the videos from highest view count to lowest, ...

Consolidating a COUNT query

I have a page where I am running an initial SQL query to get a list of subjects, then I loop over this query and run two additional queries for each record returned from the original subjects query (I ...

R: Count number of objects in list [closed]

Can someone recommend a function that can allow me to count and return the number of items in a list? library(stringr) l <- strsplit(words, "a") if(# number of items in list l < 1) ...

Mysql get count of rows for each day

My Current query is: SELECT DISTINCT DATE(vote_timestamp) AS Date, COUNT(*) AS TotalVotes FROM `votes` WHERE vote_target_id= 83031 GROUP BY DATE(vote_timestamp) ORDER BY DATE(vote_timestamp) DESC ...

Group by named column

I always forget how to do things like this. I have a database table with birthdates and I want to find out how many people have the same age. I m trying: SELECT TIMESTAMPDIFF( YEAR, birthdate, ...

TableView oval button for Index/counts

Can someone help me create an index/count button for a UITableView, like this one? iTunes http://img.skitch.com/20091107-nwyci84114dxg76wshqwgtauwn.preview.jpg Is there an Apple example, or other ...

热门标签