我一直在嘗試爲此獲取解決方案,但仍無法找到適當的解決方案。 我參考了幾個鏈接Nested for-each loops, accessing outer element with variable from the inner loop和How to extract child tags text and extended text of parent tag from xml using xslt但這些問題沒有嵌套標籤。XSLT:如何根據依賴於其子標記的值的條件獲取父標記的值,並使用高度嵌套的XML
我的XML:
<catalog title="TitleABC1">
<cd>
<title code="Y">Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
</cd>
<catalog title="TitleABC2">
<cd>
<cd>
<title code="N">Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
</cd>
</cd>
<cd>
<cd>
<cd>
<title code="Y">Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
</cd>
</cd>
</cd>
<cd>
<catalog title="TitleABC3">
<cd>
<title code="N">Red</title>
<artist>The Communards</artist>
<country>UK</country>
</cd>
</catalog>
</cd>
<cd>
<title code="N">Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
</cd>
</catalog>
</catalog>
對於上面的XML,條件是,只有那些catalog
標籤的標題會如果有孩子/後裔title
標籤具有代碼屬性爲「Y」來顯示。
因此,輸出應該是這樣的:
TitleABC1
TitleABC2
我試圖在以下邏輯XSLT,但無法獲得所需的解決方案。
<xsl:template match="catalog">
<!-- Store the value in a variable -->
<xsl:for-each select="//title">
<!-- <xsl:if> to check for the code attrib -->
</xsl:for-each>
</xsl:template>
由於您是通過解釋您如何找到答案來解答問題的,請讓我評論一下您的問題解決策略。在網絡上搜索與您的英文語言描述相匹配的現有代碼是不太可能的:這有點像搜索「添加3到17」,這將無法找到成功添加4到18的代碼。這將是更好地投入精力來理解語言的基本操作,例如路徑表達式,座標軸和過濾器表達式,並學習如何自己組合它們。 –
是@MichaelKay,你是對的。我不知道路徑表達式,軸。當然,我會更多地考慮這些主題並研究它們。 – CuE