簡體   English   中英

如何使用 XSLT 基於輸入 XML 請求的相同屬性從 XML 文件中刪除公共節點?

[英]How to remove common nodes from XML files based on same attributes of input XML request using XSLT?

我有兩個 XML:

XML1.xml將作為輸入請求。

<ConnectorConfig>
<service uri="/gen5">
<routeUrl>http://localhost:3003/v1</routeUrl>
</service>
<service uri="/gen6">
<routeUrl>http://localhost:3003/v61</routeUrl>
</service>
</ConnectorConfig> 

XML2.xml將存儲 xml 文件。

<ConnectorConfig>
<service uri="/gen5">
<routeUrl>http://localhost:3003/v5</routeUrl>
</service>
<service uri="/gen6">
<routeUrl>http://localhost:3003/v6</routeUrl>
</service>
<service uri="/gen7">
<routeUrl>http://localhost:3003/v7</routeUrl>
</service>
<service uri="/gen8">
<routeUrl>http://localhost:3003/v7</routeUrl>
</service>
</ConnectorConfig>

我需要的Output是獲取 XML2.xml 的所有元素,但在XML1.xml中具有相同服務屬性的元素除外

所需的 Output:

<ConnectorConfig>
    <service uri="/gen7">
        <routeUrl>http://localhost:3003/v7</routeUrl>
    </service>
    <service uri="/gen8">
        <routeUrl>http://localhost:3003/v7</routeUrl>
    </service>
</ConnectorConfig>

這是我嘗試過的。

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ConnectorConfig">
  <xsl:copy> 
     <xsl:variable name="item" select="document('XML2.xml')/ConnectorConfig"/>  
     <xsl:variable name="current" select="."/> 

   <xsl:for-each select="$item/service"> 
      <xsl:variable name="savedUri" select="@uri"/> 
 
        <xsl:if test="$current/service[@uri!=$savedUri]">
          <xsl:apply-templates select="."/>
        </xsl:if> 

   </xsl:for-each>  
  </xsl:copy>
  </xsl:template>

 </xsl:stylesheet>

我為輸入請求XML1.xml 得到的 Output是:

<ConnectorConfig>
    <service uri="/gen5">
        <routeUrl>http://localhost:3003/v5</routeUrl>
    </service>
    <service uri="/gen6">
        <routeUrl>http://localhost:3003/v6</routeUrl>
    </service>
    <service uri="/gen7">
        <routeUrl>http://localhost:3003/v7</routeUrl>
    </service>
    <service uri="/gen8">
        <routeUrl>http://localhost:3003/v7</routeUrl>
    </service>
</ConnectorConfig> 

但是當我嘗試使用輸入XML1a.xml作為相同的實現時:

<ConnectorConfig>
<service uri="/gen5">
<routeUrl>http://localhost:3003/v1</routeUrl>
</service>
</ConnectorConfig> 

我收到了所需的 output,如下所示。

<ConnectorConfig>
    <service uri="/gen6">
        <routeUrl>http://localhost:3003/v6</routeUrl>
    </service>
    <service uri="/gen7">
        <routeUrl>http://localhost:3003/v7</routeUrl>
    </service>
    <service uri="/gen8">
        <routeUrl>http://localhost:3003/v7</routeUrl>
    </service>
</ConnectorConfig>

在上面,output 結果包含 XML2.xml 的所有元素,除了一個具有/gen5作為輸入XML1a.Z0F635D0E0F3874FFF8B581C132E6C7中服務元素的公共屬性的元素。

從上面我知道的是,對於輸入請求中的單個服務元素,我得到了想要的結果,但是對於多個元素它失敗了。

我在做什么錯誤? 請幫忙。

簡單地說:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="ConnectorConfig">
    <xsl:variable name="local-uris" select="service/@uri" />
    <xsl:copy> 
        <xsl:copy-of select="document('XML2.xml')/ConnectorConfig/service[not(@uri=$local-uris)]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

請注意謂詞:

[not(@uri=$local-uris)]

一樣:

[@uri!=$local-uris]

前者僅傳遞uri$local-uris中沒有匹配值的節點。

后者傳遞任何uri不等於$local-uris中至少一個uri的節點。 在給定的示例中,所有節點都通過了此測試。 如果將$local-uris減少為單個值,則所有節點都將通過此測試,除了等於單個值的節點。 這應該解釋你自己的觀察。

暫無
暫無

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

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