[xsl] XML access control by custom ID

Subject: [xsl] XML access control by custom ID
From: Jacobus Reyneke <jacobusreyneke@xxxxxxxxx>
Date: Tue, 16 Mar 2010 10:31:44 +0200
Thanks a great deal Micheal.

No response required on this post. It's just for information:

Honestly, I have not been able to get your solution to work. It copies
the input file "as is". The solution provided by AC does the trick,
but it would be interesting to see your solution at work also. I tried
fixing ;-) your solution, but I've never worked with keys before, so I
will come back to it, once I have keys under the belt.


---------Test input file:---------
Access controlled content (data.xml):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a some_attributes="xyz" my_id="1">
  123
</a>
<b attribute="xxx" my_id="2">
  ABC
</b>
</root>

----------User role access control settings (access.xml)---------
<?xml version="1.0" encoding="UTF-8"?>
<userroles>
  <administrator>
     <read>
        <my_id>1</my_id>
        <my_id>2</my_id>
     </read>
     <write>
        <my_id>1</my_id>
        <my_id>2</my_id>
     </write>
  </administrator>
  <anybody>
     <read>
        <my_id>1</my_id>
     </read>
     <write>
        <my_id>2</my_id>
     </write>
  </anybody>
</userroles>


-----------Your stylesheet:--------
<xsl:stylesheet version="2.0"
 xmlns:xs="http://www.w3.org/2001/XMLSchema";
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:param name="username" as="xs:string" select="'anybody'" />

 <xsl:variable name="acl-doc" as="document-node()"
 select="doc( 'access.xml' )"/>

 <xsl:variable name="accessible" as="xs:string*"
 select="$acl-doc/*/*[local-name() = $username]/*/my_id"/>

 <!-- LKP: make up key by concatenating username, separator, id -->
 <xsl:key name="right-for-id" match="my_id"
 use="concat( ../../local-name(), '--', . )"/>

 <xsl:template match="users/*"/><!-- ignore by default -->

 <!-- process accessible nodes: copy and add @access -->
 <xsl:template match="users/*[ @my_id = $accessible ]" priority="1">
 <xsl:copy>
 <xsl:copy-of select="@*"/>
 <!-- LKP: as above -->
 <xsl:variable name="lkp-key"
  select="concat( $username, '--', @my_id )"/>
 <xsl:attribute name="access"
  select="key( 'right-for-id', $lkp-key, $acl-doc )/../local-name()"/>
 <xsl:apply-templates/>
 </xsl:copy>
 </xsl:template>

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

</xsl:stylesheet>

------------Expected results:-------------
<?xml version="1.0" encoding="UTF-8"?>
<a access="read" some_attributes="xyz" my_id="1">
  123
</a>
<b access="write" attribute="xxx" my_id="2">
  ABC
</b>

-------Actual results:---------
A copy of data.xml "as is"

Unfortunately I don't understand your solution (yet) so it's difficult to
debug.

Kind regards,
Jacobus

Current Thread