I am trying to do a simple “find-replace” in xslt but having trouble getting it to produce the correct output. I tried to follow other examples but unfortunately I haven’t done much with XSLT and haven’t been able to locate an example which does what I want to do.
I have the following XML:
<metadata>
<Permissions>site:abcdefg-123456-id1::{azeckoski}h,S,r</Permissions>
<Permissions>site:hjklqwe-098765-id2::{person2}h,S,r</Permissions>
<Permissions>playlist:5678::{azeckoski}h,S,r</Permissions>
<Permissions>playlist:7890::{azeckoski}H,s,r</Permissions>
<Permissions>playlist:1234::{person1}H,s,R</Permissions>
</metadata>
And the following XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:package="info:srw/extension/13/package-v1.0" >
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="metadata[starts-with(Permissions,'site:abcdefg-123456-id1::')]">
<xsl:copy>
<xsl:text>site:abcdefg-123456-id1::{azeckoski}H,S,R</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I would like to end up with the following XML:
<metadata>
<Permissions>site:abcdefg-123456-id1::{azeckoski}H,S,R</Permissions>
<Permissions>site:hjklqwe-098765-id2::{person2}h,S,r</Permissions>
<Permissions>playlist:5678::{azeckoski}h,S,r</Permissions>
<Permissions>playlist:7890::{azeckoski}H,s,r</Permissions>
<Permissions>playlist:1234::{person1}H,s,R</Permissions>
</metadata>
However I end up with this instead:
<metadata>site:abcdefg-123456-id1::{azeckoski}H,S,R</metadata>
I don’t understand why the rest of the XML document is being wiped out. I thought I was using the identity template to keep everything else as it is.
Use:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Your problem was that you do not process (apply templates to) the children of
metadata.