2016-08-22 61 views
-1

XSLT這應該是相當簡單的,我有一些XML:過濾XML使用屬性值

<?xml version="1.0" encoding="UTF-8"?> 
<!--Arbortext, Inc., 1988-2012, v.4002--> 
<?Pub Inc?> 
<chapter role="h1" xml:id="chapter_N3944673" xmlns="http://docbook.org/ns/docbook"> 
    <title>Release Notes</title> 
    <dynamic_block role="ta_TopicAlias">ReleaseNotes</dynamic_block> 
    <sidebar outputformat="html_only"> 
     <para condition="wip">Release Preview Draft</para> 
     <para>Revision Date: August 17, 2016</para> 
    </sidebar> 

這只是一個片段,您可能會注意到它的Docbook格式。我想刪除所有具有@condition值的para節點。所以我這樣做了:

<?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output method="xml" indent="yes"/> 
     <xsl:template match="//para[@condition]" /> 
     <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     </xsl:template> 
</xsl:stylesheet> 

哪個沒有做什麼。我也試過// @條件,它只是刪除屬性並保持節點完好無損。此外,para可以出現在其他標籤內的整個文檔中。

如果XSL不是我接受其他想法的最佳方法。

+0

看看是否有幫助:http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 –

+0

這是一個經常遇到的項目來到XML文檔。基本上,你有一個未聲明的名字空間需要一個分配的前綴。 – Parfait

回答

0

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<!--Arbortext, Inc., 1988-2012, v.4002--> 
<?Pub Inc?> 
<chapter role="h1" xml:id="chapter_N3944673" xmlns="http://docbook.org/ns/docbook" > 
    <title>Release Notes</title> 
    <dynamic_block role="ta_TopicAlias">ReleaseNotes</dynamic_block> 
    <sidebar outputformat="html_only"> 
     <para condition="wip">Release Preview Draft</para> 
     <para>Revision Date: August 17, 2016</para> 
    </sidebar> 

</chapter> 

XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns="http://docbook.org/ns/docbook" > 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="*[namespace-uri() = 'http://docbook.org/ns/docbook'] 
         [local-name()='para'] 
         [@condition]"/> 
    <!--In above line of code, first, finding the element which is related to 
     docbook namespace, then identifying the element by name 'para', 
     and then identify the element which is having 'condition' as attribute --> 

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

</xsl:stylesheet> 

輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<!--Arbortext, Inc., 1988-2012, v.4002--> 
<?Pub Inc?> 
<chapter xmlns:db="http://docbook.org/ns/docbook" role="h1" xml:id="chapter_N3944673"> 
    <title>Release Notes</title> 
    <dynamic_block role="ta_TopicAlias">ReleaseNotes</dynamic_block> 
    <sidebar outputformat="html_only"> 

     <para>Revision Date: August 17, 2016</para> 
    </sidebar> 

</chapter> 

再答案來自:Thiyagaraj答案。

丟棄下面答案,因爲這是適用於XSLT 2.0 變化

<xsl:template match="//para[@condition]" /> 

<xsl:template match="*:para[@condition]" /> 

因爲,在XML根名稱空間使用xmlns="http://docbook.org/ns/docbook"

+0

謝謝但Notepad ++告訴我,當我這樣做時,XSL無效。 – Dan

+1

這個「解決方案」需要XSLT 2.0。忽略命名空間並不是一個好習慣。 –

+0

好吧,現在就明白了。我將命名空間添加到XSLT,並且現在正在進行適當的匹配。 – Dan