2017-09-14 71 views
0

重複節點狀態我有要求使用XSLT從這種一般形式的XML文件中選擇值如何選擇元素的值:基於其中從在XSLT

<compartment> 
<receipt> 
    <Measurements> 
     <Measurement> 
      <Identifier>temperature>/Identifier> 
      <Quantity>30.0</Quantity> 
     </Measurement> 
     <Measurement> 
      <Identifier>density>/Identifier> 
      <Quantity>30.0</Quantity> 
     </Measurement> 
    </Measurements> 
</receipt> 
<receipt> 
    <Measurements> 
     <Measurement> 
      <Identifier>volume>/Identifier> 
      <Quantity>30.0</Quantity> 
     </Measurement> 
     <Measurement> 
      <Identifier>density>/Identifier> 
      <Quantity>30.0</Quantity> 
     </Measurement> 
    </Measurements> 
</receipt> 
<compartment> 

具體而言,我要提取的值的<數量>元素的兄弟<標識符>具有值「溫度」。我的問題是,我不知道如何選擇只包含溫度值的<收據(本例中的第一個)。

我試着用這個XSLT,但它不能正常工作:

<Output> 
    <xsl:value-of select="normalize-space(compartment/receipt[Measurements/pidx:Measurement[Identifier='Temperature']/Measurements/Measurement[pidx:UnitQualifier='ProductWeight']/Quantity)"/> 
</Output> 

我該如何解決呢?

回答

1

鑑於以下合式輸入:

XML

<compartment> 
    <receipt> 
    <Measurements> 
     <Measurement> 
     <Identifier>temperature</Identifier> 
     <Quantity>30.0</Quantity> 
     </Measurement> 
     <Measurement> 
     <Identifier>density</Identifier> 
     <Quantity>31.0</Quantity> 
     </Measurement> 
    </Measurements> 
    </receipt> 
    <receipt> 
    <Measurements> 
     <Measurement> 
     <Identifier>volume</Identifier> 
     <Quantity>32.0</Quantity> 
     </Measurement> 
     <Measurement> 
     <Identifier>density</Identifier> 
     <Quantity>33.0</Quantity> 
     </Measurement> 
    </Measurements> 
    </receipt> 
</compartment> 

以下樣式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/"> 
    <Output> 
     <xsl:value-of select="compartment/receipt/Measurements/Measurement[Identifier='temperature']/Quantity"/> 
    </Output> 
</xsl:template> 

</xsl:stylesheet> 

將返回:

<?xml version="1.0" encoding="UTF-8"?> 
<Output>30.0</Output> 

注意XML是大小寫敏感的:'Temperature'是不一樣的東西'temperature'