I need to iterate through an XML file. The root node has a few children and I need to either copy the child as is or to do something. So I'm working on an XSLT to do so. Here's a sample source XML:
<?xml version="1.0" encoding="utf-8"?>
<XDSDocumentEntry id="DOC01">
<author authorRole="XDSITEST_DICOM_INSTANCE_PUBLISHER" authorPerson="XDSITEST">Author</author>
<classCode displayName="Communication" codingScheme="Connect-a-thon classCodes">Communication</classCode>
<confidentialityCode displayName="Celebrity" codingScheme="Connect-a-thon confidentialityCodes">
</XDSDocumentEntry>
In this XML I need to select nodes author, classCode and confidentialityCodes but I'm getting the text() nodes with this code:
<xsl:for-each select="node()"><!--<xsl:copy-of select="."/>-->
<!--<xsl:value-of select="local-name()"/>-->
<xsl:choose>
<xsl:when test="author">
do something
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
My result so far is this:
author<author authorRole="XDSITEST_DICOM_INSTANCE_PUBLISHER" authorPerson="XDSITEST"
authorInstitution="Some institution"/>
classCode<classCode displayName="Communication" codingScheme="Connect-a-thon classCodes">Communication</classCode>
confidentialityCode<confidentialityCode displayName="Celebrity" codingScheme="Connect-a-thon confidentialityCodes">
C</confidentialityCode>
Any hint? Thx.
EDIT
Sorry, had an error (I removed ).
Actually, why am I using the for-each is because I need the document exactly as it was except for a few nodes. In the example above the final output should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<XDSDocumentEntry>
<author authorRole="XDSITEST_DICOM_INSTANCE_PUBLISHER" authorPerson="XDSITEST"
authorInstitution="Some institution"/>
<author>
<authorInstitution>
<organizationName>Some institution</organizationName>
</authorInstitution>
<authorRole>XDSITEST_DICOM_INSTANCE_PUBLISHER</authorRole>
<authorPerson>
<assigningAuthorityName>XDSITEST</assigningAuthorityName>
</authorPerson>
</author>
<classCode displayName="Communication" codingScheme="Connect-a-thon classCodes">Communication</classCode>
<confidentialityCode displayName="Celebrity" codingScheme="Connect-a-thon confidentialityCodes">
C</confidentialityCode>
</XDSDocumentEntry>
EDIT 2
I created this template as suggested by @Martin. But still how do I select the node name 'author'??
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:choose>
<xsl:when test="local-name()=author">
a
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>