簡體   English   中英

動態 XPath 使用 XSLT 過濾 XML 文檔

[英]Dynamic XPath to filter XML documents using XSLT

我一直在搜索 StackOverflow 一段時間但沒有成功。 我的目標是過濾 Employee XML 文檔。 輸入 XML 有大約 9000 名員工,我有多個目標系統,他們需要根據每個目標系統的不同選擇標准來獲取這些員工記錄的子集。 樣本輸入:

<Employees>
  <Employee>
    <id>1</id>
    <name>Name1</name>
  </Employee>
  <Employee>
    <id>2</id>
    <name>Name2</name>
  </Employee>
  <Employee>
    <id>3</id>
    <name>Name3</name>
  </Employee>
  <Employee>
    <id>4</id>
    <name>Name4</name>
  </Employee>
</Employees>

output 應該具有相同的結構,但根據選擇標准 (XPATH) 減少了節點。 如何將動態 XPath 傳遞給 XSLT 樣式表並將過濾后的員工設為 output? 請注意完整的 Xpath 表達式以及過濾條件作為一個字符串傳遞。

使用 XSLT 3.0, xsl:evaluate用於動態 XPath 評估。 您可以接受 XPath 作為xsl:param (顯示示例默認 XPath 值),然后對其進行評估。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:output indent="yes"/>
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:param name="path" select="'/Employees/Employee[number(id) gt 2]'" />
        
    <xsl:template match="/">
        <xsl:variable name="var" as="node()*">
            <xsl:evaluate xpath="$path" context-item="."/>
        </xsl:variable>
        <xsl:apply-templates select="$var"/>
    </xsl:template>

</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM