xslt - Using XSL/XPath to match nodes with any name having a given attribute and child-node -


i'm trying use xpath/xslt add node existing node fulfills requirements:

  • the node has attribute "id"
  • the node has child named "type", containing given text, e.g. "identifier"

in xml match:

  <somerandomnode>     <type>somerandomtype</type>     <child>       <count type="int32">2</count>              <!-- node should matched -->       <key id="5">         <type>identifier</type>         <somevalue type="string">hello</somevalue>         <someothervalue type="string">world</someothervalue>       </key>     </child>   </somerandomnode> </project> 

i'm having hard time writing match expression this, "best" attempt being:

<xsl:template match="*[@id][.//typename='identifier']">    <xsl:copy>     <xsl:attribute name="id">       <xsl:value-of select="@id"/>     </xsl:attribute>      <!-- copy nodes -->     <xsl:copy-of select="type" />     <xsl:copy-of select="somevalue" />     <xsl:copy-of select="someothervalue" />     <!-- add new -->     <newvalue type="string">this node added</newvalue>   </xsl:copy> </xsl:template> 

if replace * nodename works fine, need match nodes name.

your template match looking descendant typename elements, want type elements.

also, matching descendants, question , template logic looking child elements.

you should adjust template match to:

*[@id][type='identifier'] 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -