Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have XML

<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<inquiryAbouts>
<inquiryAbout>
<code>Code</code>
<nameKk>Something</nameKk>
<nameRu>Something</nameRu>
<documents xsi:nil="true"/>
</inquiryAbout>
</inquiryAbouts>
</getInquiryAboutListReturn>

And I want to process it with XSLT to copy all XML

How could I copy all XML without <documents xsi:nil="true"/> or without xsi:nil="true"?

Desired output XML

<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<inquiryAbouts>
<inquiryAbout>
<code>Code</code>
<nameKk>Something</nameKk>
<nameRu>Something</nameRu>
</inquiryAbout>
</inquiryAbouts>
</getInquiryAboutListReturn>
share|improve this question

closed as off topic by Michael Hampton, mdpc, MDMarra, Iain Dec 1 '12 at 19:38

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Welcome to Server Fault! Your question is off topic for Serverfault because it appears to relate programming. It may be on topic for StackOverflow but please search their site for similar questions that may already have the answer you're looking for. –  Iain Dec 1 '12 at 19:39

1 Answer 1

up vote 1 down vote accepted

This simple XSLT:

<?xml version="1.0"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  version="1.0">

  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <!-- TEMPLATE #2 -->
  <xsl:template match="*[@xsi:nil = 'true']" />

</xsl:stylesheet>

...when applied to the OP's source XML:

<?xml version="1.0"?>
<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <inquiryAbouts>
    <inquiryAbout>
      <code>Code</code>
      <nameKk>Something</nameKk>
      <nameRu>Something</nameRu>
      <documents xsi:nil="true"/>
    </inquiryAbout>
  </inquiryAbouts>
</getInquiryAboutListReturn>

...produces the expected result XML:

<?xml version="1.0"?>
<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <inquiryAbouts>
    <inquiryAbout>
      <code>Code</code>
      <nameKk>Something</nameKk>
      <nameRu>Something</nameRu>
    </inquiryAbout>
  </inquiryAbouts>
</getInquiryAboutListReturn>

EXPLANATION:

  1. The first template -- the Identity Template -- copies all nodes and attributes from the source XML document as-is.
  2. The second template, which matches all elements with the specified, namespaced attribute equalling "true", effectively removes those elements.
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.