位置函数返回值无法索引集合中的特定节点

时间:2015-01-22 07:20:37

标签: xslt xslt-1.0

我有下面的XSLT工作正常:

  <xsl:template match="/*">
    <root>
      <xsl:variable name="AddressNames" select="myUtil:getSecondXMLData()"/>
      <xsl:for-each select="item">
        <AddressName>
          <xsl:variable name="curValue" select="position()"/>
          <xsl:value-of select="$AddressNames//Item[$curValue]/names"/>
        </AddressName>
      </xsl:for-each>
    </root>
  </xsl:template>

我的问题是,当代替使用变量来包含当前位置然后使用它来索引&#34; //项目&#34;的集合中的特定项目时,我只尝试这个

<xsl:value-of select="$AddressNames//Item[position()]/names"/>

我没有得到所需的输出...... 期望的输出:

<root>
    <AddressName>rahul sharma</AddressName>
    <AddressName>rahul sharma</AddressName>
    <AddressName>rahul sharma1</AddressName>
</root>

收到错误的输出:

<root xmlns:myUtil="pda:MyUtils">
    <AddressName>rahul sharma</AddressName>
    <AddressName>rahul sharma</AddressName>
    <AddressName>rahul sharma</AddressName>
</root>

输入是:

<?xml version="1.0" encoding="iso-8859-1"?>
<address>
  <item p2:type="ns1:sboeconnectCustomerAddressEntityItem" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
    <customer_address_id p2:type="xsd:string">19221</customer_address_id>
    <created_at p2:type="xsd:string">2015-01-21 13:11:13</created_at>
    <updated_at p2:type="xsd:string">2015-01-21 13:11:13</updated_at>
    <city p2:type="xsd:string">los angeles</city>
    <company p2:type="xsd:string">insync</company>
    <country_id p2:type="xsd:string">US</country_id>
    <firstname p2:type="xsd:string">rahul</firstname>
    <lastname p2:type="xsd:string">sharma</lastname>
    <postcode p2:type="xsd:string">90066</postcode>
    <region p2:type="xsd:string">Alabama@AL</region>
    <street p4:arrayType="xsd:string[1]" p2:type="ns1:ArrayOfString" xmlns:p4="http://schemas.xmlsoap.org/soap/encoding/">
      <item p2:type="xsd:string">eeee</item>
    </street>
    <telephone p2:type="xsd:string">8065534367</telephone>
    <is_default_billing p2:type="xsd:boolean">true</is_default_billing>
    <is_default_shipping p2:type="xsd:boolean">true</is_default_shipping>
  </item>
  <item>
    <customer_address_id>19221</customer_address_id>
    <city>los angeles</city>
    <country_id>US</country_id>
    <region>Alabama@AL</region>
    <firstname>rahul</firstname>
    <lastname>sharma</lastname>
    <postcode>90066</postcode>
    <street>
      <item p4:type="xsd:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">eeee</item>
    </street>
    <telephone>8065534367</telephone>
    <is_default_billing>false</is_default_billing>
    <is_default_shipping>true</is_default_shipping>
  </item>
  <item p2:type="ns1:sboeconnectCustomerAddressEntityItem" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
    <customer_address_id p2:type="xsd:string">19222</customer_address_id>
    <created_at p2:type="xsd:string">2015-01-21 13:11:13</created_at>
    <updated_at p2:type="xsd:string">2015-01-21 13:11:13</updated_at>
    <city p2:type="xsd:string">los angeles</city>
    <company p2:type="xsd:string">insync</company>
    <country_id p2:type="xsd:string">US</country_id>
    <firstname p2:type="xsd:string">rahul</firstname>
    <lastname p2:type="xsd:string">sharma</lastname>
    <postcode p2:type="xsd:string">90066</postcode>
    <region p2:type="xsd:string">Alabama@AL</region>
    <street p4:arrayType="xsd:string[1]" p2:type="ns1:ArrayOfString" xmlns:p4="http://schemas.xmlsoap.org/soap/encoding/">
      <item p2:type="xsd:string">CB-28</item>
    </street>
    <telephone p2:type="xsd:string">8065534367</telephone>
    <is_default_billing p2:type="xsd:boolean">false</is_default_billing>
    <is_default_shipping p2:type="xsd:boolean">false</is_default_shipping>
  </item>
</address>

变量(AddressNames)包含以下信息:

<?xml version="1.0" encoding="UTF-8"?>
<AddressNameCollection>
  <Item>
    <names>rahul sharma</names>
  </Item>
  <Item>
    <names>rahul sharma</names>
  </Item>
  <Item>
    <names>rahul sharma1</names>
  </Item>
</AddressNameCollection>

1 个答案:

答案 0 :(得分:-2)

不确定你究竟在问什么。在XPath中,谓词过滤由位置步骤选择的节点集。表达式中的谓词[2]

/root/item[2]

过滤项目。它相当于:

/root/item[position() = 2]

其中position()被评估为root/item选择的项目的节点集中项目的位置。表达式:

/root/item[position()]

评估为:

/root/item[position() = position()]

这当然会对任何项目返回true,因此这里的谓词没有做任何事情。

你要做的是:

/root/item[position() = current()/position()]

哪个在XSLT 2.0(我认为)中工作,但在XSLT 1.0中没有。在XSLT 1.0中,您必须将上下文项的位置存储在变量中,并在谓词中使用该变量(正如您在开头示例中所做的那样)。