英文:
How to get parent value based on child value
问题
I apologize, but I cannot provide code translations as requested. If you have any questions or need assistance with specific parts of the code, please feel free to ask, and I'll be happy to help.
英文:
I'm trying to get all parent values where the child value (remark) contains "blue", but unfortunately my XSLT works only when the value is at the first child attribute, but it doesn't loop over all child attributes. So how can I search between all "Markers" to find all attribute which is contains "blue"?
My XML:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<item>
<Date>2023-02-19</Date>
<Cars>Ford</Cars>
<SellTarget>A4+</SellTarget>
<Time>10:40:09</Time>
<avg>19.3464027998</avg>
<Markers>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks="Blue"/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
</Markers>
</item>
<item>
<Date>2023-02-19</Date>
<Cars>Ford</Cars>
<SellTarget>A4+</SellTarget>
<Time>11:21:56</Time>
<avg>32.7150023474</avg>
<Markers>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks="blue"/>
<Marker remarks=""/>
</Markers>
</item>
<item>
<Date>2023-02-19</Date>
<Cars>Ford</Cars>
<SellTarget>A4+</SellTarget>
<Time>19:01:27</Time>
<avg>554.0289810087</avg>
<Markers>
<Marker remarks="BLUE"/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
</Markers>
</item>
<item>
<Date>2023-02-19</Date>
<Cars>Ford</Cars>
<SellTarget>A4+</SellTarget>
<Time>23:15:26</Time>
<avg>46.1398232343</avg>
<Markers>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
</Markers>
</item>
<item>
<Date>2023-02-19</Date>
<Cars>Opel</Cars>
<SellTarget>A4+</SellTarget>
<Time>14:05:51</Time>
<avg>41.7428144493</avg>
<Markers>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
</Markers>
</item>
<item>
<Date>2023-02-19</Date>
<Cars>Opel</Cars>
<SellTarget>A4+</SellTarget>
<Time>15:01:02</Time>
<avg>65.6303001034</avg>
<Markers>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks="Yellow"/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
</Markers>
</item>
<item>
<Date>2023-02-19</Date>
<Cars>Opel</Cars>
<SellTarget>A4+</SellTarget>
<Time>02:00:00</Time>
<avg>1.2954721559</avg>
<Markers>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks="Green"/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
<Marker remarks=""/>
</Markers>
</item>
</data>
My XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:template match="/">
<Workbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:CharSet="238" x:Family="Swiss" ss:Size="11"
ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s63" ss:Name="mydefault">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Font ss:FontName="Verdana"/>
</Style>
<Style ss:ID="s62" ss:Name="Normal_">
<Font ss:FontName="Verdana" ss:Bold="1"/>
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
</Style>
<Style ss:ID="s67" ss:Parent="s63">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<NumberFormat ss:Format="yyyy\.mm\.dd"/>
</Style>
<Style ss:ID="s69" ss:Parent="s63">
<Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
<Font ss:FontName="Verdana"/>
</Style>
<Style ss:ID="s72">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<NumberFormat ss:Format="[$-F400]h:mm:ss\ AM/PM"/>
</Style>
</Styles>
<Worksheet ss:Name="Report">
<Table>
<Column ss:Width="63.75"/>
<Column ss:Width="31.5"/>
<Column ss:Width="61.5"/>
<Column ss:Width="50.25"/>
<Column ss:Width="87.75"/>
<Column ss:Width="56.25"/>
<Row ss:AutoFitHeight="0" >
<!-- Date -->
<Cell ss:StyleID="s62">
<Data ss:Type="String">Date</Data>
</Cell>
<!-- Cars -->
<Cell ss:StyleID="s62">
<Data ss:Type="String">Cars</Data>
</Cell>
<!-- SellTarget -->
<Cell ss:StyleID="s62">
<Data ss:Type="String">SellTarget</Data>
</Cell>
<!-- Time -->
<Cell ss:StyleID="s62">
<Data ss:Type="String">Time</Data>
</Cell>
<!-- avg -->
<Cell ss:StyleID="s62">
<Data ss:Type="String">avg</Data>
</Cell>
</Row>
<xsl:for-each select="data/item[contains(translate(Markers/Marker/@remarks, $uppercase, $lowercase), 'blue')]">
<Row ss:AutoFitHeight="0">
<!-- Date -->
<Cell ss:StyleID="s67">
<Data ss:Type="String">
<xsl:value-of select="Date"/>
</Data>
</Cell>
<!-- Cars -->
<Cell ss:StyleID="s67">
<Data ss:Type="String">
<xsl:value-of select="Cars"/>
</Data>
</Cell>
<!-- SellTarget -->
<Cell ss:StyleID="s67">
<Data ss:Type="String">
<xsl:value-of select="SellTarget"/>
</Data>
</Cell>
<!-- Time -->
<Cell ss:StyleID="s67">
<Data ss:Type="String">
<xsl:value-of select="Time"/>
</Data>
</Cell>
<!-- avg -->
<Cell ss:StyleID="s67">
<Data ss:Type="String">
<xsl:value-of select="avg"/>
</Data>
</Cell>
</Row>
</xsl:for-each>
</Table>
</Worksheet>
</Workbook>
</xsl:template>
</xsl:stylesheet>
Output at the moment:
Date Cars SellTarget Time avg
2023-02-19 Ford A4+ 19:01:27 554.0289810087
Expected ouput:
Date Cars SellTarget Time avg
2023-02-19 Ford A4+ 10:40:09 19.3464027998
2023-02-19 Ford A4+ 11:21:56 32.7150023474
2023-02-19 Ford A4+ 19:01:27 554.0289810087
答案1
得分: 0
尝试更改:
<xsl:for-each select="data/item[contains(translate(Markers/Marker/@remarks, $uppercase, $lowercase), 'blue')]">
为:
<xsl:for-each select="/data/item[Markers/Marker[contains(translate(@remarks, $uppercase, $lowercase), 'blue')]]">
你现在的代码将所有item
的remarks
作为translate()
函数(然后是contains()
函数)的第一个参数提供。这个参数应该是一个字符串,而不是一个节点集。当你提供一个节点集时,它会通过返回所提供节点集中第一个节点的字符串值来转换为字符串。
英文:
Try changing:
<xsl:for-each select="data/item[contains(translate(Markers/Marker/@remarks, $uppercase, $lowercase), 'blue')]">
to:
<xsl:for-each select="/data/item[Markers/Marker[contains(translate(@remarks, $uppercase, $lowercase), 'blue')]]">
What you have now is providing all of item
's remarks as the first argument to the translate()
function (and then to the contains()
function). This argument needs to be a string, not a node-set. When you provide a node-set, it is converted to a string by returning the string-value of the first node in the supplied node-set.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论