簡體   English   中英

如何根據其他節點的屬性值刪除 XML 節點

[英]How to remove XML nodes depending attribute value of other nodes

我有一個 XML 看起來像這樣:

  <pack>
    <titlesPacks>
      <StoryPack id="1111111">
        <value>A</value>
      </StoryPack>
      <StoryPack id="2222222">
        <value>F</value>
      </StoryPack>
    </titlesPacks>
    <referenceTable>
       <TitleReference id="1111111" />
       <TitleReference id="2222222" />
    </referenceTable>
  </pack>

我需要復制 xml 文件,但是:

  • 刪除value節點具有特定值的StoryPack節點(例如 A、B、C)。 這部分已經OK

  • delete 'TitleReference nodes where id attribute value is equal to id attribute value of' StoryPack節點在上面刪除

我不知道如何做第二個:我嘗試使用 Key,但不起作用:

我當前的 XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:data="http://example.com/data" exclude-result-prefixes="data">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="titleId" match="StoryPack" use="@id"/>

    <!-- Values for wich nodes must be deleted -->
    <data:data xmlns="">
        <value>A</value>
        <value>B</value>
        <value>C</value>
    </data:data>

    <xsl:variable name="values" select="document('')/xsl:stylesheet/data:data/value"/>


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


<!-- Delete nodes with specific values -->
    <xsl:template match="StoryPack[value = $values]"/>

<!-- Dlete nodes with id from specific values -->
<xsl:template match="TitleReference[StoryPack[key('titleId', @id)/value = $values]]" />
</xsl:stylesheet>

謝謝您的幫助 !

==============

遵循@michael.hor257k 的答案:我的來源 XML 有點復雜,這可能就是為什么它在我的情況下不起作用:價值的 Xpath 更深:/Pack/titlesPacks/StoryPack/assets/TitleAssets/assets/ /value 所以我已經完成了:

<xsl:template match="TitleReference[key('titleId', @id)/assets/TitleAssets/assets/StringAssetInfo[@attrId = '127']/value = $values]" />

但它不起作用。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Pack>
  <titlesPacks>
    <StoryPack id="1111111">
      <assets>
        <TitleAssets>
             <assets>
               <StringAssetInfo attrId="127">
                  <value>A</value>
               </StringAssetInfo>
             </assets>
        </TitleAssets>
      </assets>
     </StoryPack>
    </titlesPacks>
    <referenceTable>
     <ReferenceTable>
      <titlesReferences>
        <TitleReference id = "1111111"/>
      </titlesReferences>
     </ReferenceTable>
    </referenceTable>
</pack>

我需要的是

  • 刪除StoryPack ,其中StringAssetInfo attrId 127 且value = A,或 B... => 已經確定
  • 刪除TitleReference其中id屬性值 = 上面已刪除節點的id屬性值

試試這個方法?

XSLT 2.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:strip-space elements="*"/>

<xsl:key name="titleId" match="StoryPack" use="@id"/>

<!-- Values for which nodes must be deleted -->
<xsl:variable name="values">
    <value>A</value>
    <value>B</value>
    <value>C</value>
</xsl:variable>

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

<!-- Delete nodes with specific values -->
<xsl:template match="StoryPack[value = $values/value]"/>

<!-- Delete nodes with id from specific values -->
<xsl:template match="TitleReference[key('titleId', @id)/value = $values/value]" />

</xsl:stylesheet>

我已經簡化了變量定義,所以它可以在這個在線演示中工作: https://xsltfiddle.liberty-development.net/jz1PuP9 - 也因為它更簡單。


添加:

鑒於您更新的 XML 結構,更改:

<xsl:template match="StoryPack[value = $values/value]"/>

至:

<xsl:template match="StoryPack[.//value = $values/value]"/>

或(最好)到:

<xsl:template match="StoryPack[assets/TitleAssets/assets/StringAssetInfo/value = $values/value]"/>

並改變:

<xsl:template match="TitleReference[key('titleId', @id)/value = $values/value]" />

至:

<xsl:template match="TitleReference[key('titleId', @id)//value = $values/value]" />

或(最好)到:

<xsl:template match="TitleReference[key('titleId', @id)/assets/TitleAssets/assets/StringAssetInfo/value = $values/value]" />

暫無
暫無

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

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