-1

This should be fairly simple, I have some 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>

That's just a fragment, you may notice it's docBook format. I want to remove all the para nodes that have a value for @condition. So I did this:

<?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>

Which doesn't do anything. I also tried //@condition which just removes the attribute and leaves the node intact. Further, para can appear all over the document inside other tags.

If XSL isn't the best approach I'm open to other ideas.

Dan
  • 266
  • 4
  • 16
  • See if this helps: http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k Aug 22 '16 at 13:36
  • This is an often encountered item when it comes to XML docs. Essentially, you have an undeclared namespace that needs an assigned prefix. – Parfait Aug 22 '16 at 14:13

1 Answers1

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>

Output:

<?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>

Referred the answer from: Thiyagaraj answer.

Discard below answer, as this is applicable in XSLT 2.0 Change

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

to

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

Because, namespace xmlns="http://docbook.org/ns/docbook" used in XML root.

Community
  • 1
  • 1
Rudramuni TP
  • 1,268
  • 2
  • 16
  • 26